Branding Custom Internet Sites in SharePoint 2010

Download Report

Transcript Branding Custom Internet Sites in SharePoint 2010

Greg Galipeau
Enterprise Architect
Department of Treasury
SharePoint provides many ways to query data.
Some ways are for external applications (web
services, rss, REST, etc…). However, some ways
are for querying within SharePoint. You can
create dynamic user interfaces by storing
configurations and data within SharePoint lists
and using these techniques to query that data.

Develop
 Server-Side
▪ CAML
▪ LINQ
 Client-Side
▪ Client Side Object Model

Out of the Box
 Content Query Web Part



SPSite = site collection
SPWeb = sub-web
SPSite.RootWeb = root sub-web
SPContext.Current.Site
vs
new SPSite(“http://sharepoint.com”)

Context
 Understands where you are at
 Disposes itself

new SPSite
 Use when opening site in another area
 You must dispose!
The following slides show many techniques for
querying lists. All the slides assume a couple
things:
1. A list called Announcements exists at the
web that the webpart is placed on
2. The Annoucements list has a title, body and
show column
//get the list
SPList mylist = SPContext.Current.Web.Lists["Announcements"];
//loop through and get the values
List<Announce> announcements = new List<Announce>();
foreach (SPListItem item in mylist.Items)
{
if (Convert.ToBoolean(item["Show"]) == true)
{
announcements.Add(
new Announce {Title = item["Title"] as string,
Body = item["Body"] as string,
Show = Convert.ToBoolean(item["Show"])
});
}
}



XMl defined querying language specific to
SharePoint
U2U CAML builder is a good place to start
learning
Same technique as SharePoint 2007 used
with SharePoint 2010
SPList mylist = SPContext.Current.Web.Lists["Announcements"];
SPQuery query = new SPQuery();
query.Query = string.Concat(
"<Where><Eq><FieldRef Name='Show'/><Value",
"Type='bit'>1</Value></Eq></Where>");
query.ViewFields = string.Concat(
"<FieldRef Name='Title' />",
"<FieldRef Name='Body' />",
"<FieldRef Name='Show' />");
query.ViewFieldsOnly = true; //only query the viewfields
SPListItemCollection items = mylist.GetItems(query);



LINQ = Language Integrated Query
Reference Microsoft.SharePoint.LINQ.dll
from the 14 hive ISAPI folder
Technically LINQ for SharePoint just
translates query into CAML



SPMetal is a command line tool that
generates the object model for a SharePoint
site
Can be found at: C:\Program Files\Common
Files\Microsoft Shared\Web Server
Extensions\14\BIN
Ex: spmetal.exe /web:http://win2008r2 /code:"C:\LINQ.cs"
// Get DataContext from page context
LINQDataContext dataContext = new
LINQDataContext(SPContext.Current.Web.Url);
// Query
var query = from a in dataContext.Announcements
orderby a.Title
where a.Show == true
select new
{
title = a.Title,
body = a.Body,
show = a.Show
};



The client object model can be used on the
server side
This technique is usually needed for external
applications like Silverlight
Reference Microsoft.SharePoint.Client.dll
and Microsoft.SharePoint.ClientRuntime.dll
from the 14 hive ISAPI folder
ClientContext clientContext = new ClientContext(SPContext.Current.Web.Url);
Web web = clientContext.Web;
List list = web.Lists.GetByTitle("Announcements");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Show'/>
<Value Type='bit'>1</Value>
</Eq>
</Where>
</Query>
</View>";
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items
.Include(
item => item["Title"],
item => item["Body"],
item => item["Show"]));
clientContext.ExecuteQuery();



The client object model can be used on the
client side with javascript
This technique is useful for no code queries
The following tag must be on your page for
this to work:
<SharePoint:ScriptLink Name="SP.js" runat="server"
OnDemand="true" Localizable="false" />

Many ways to incorporate javascript into a
SharePoint page
 Page Layout
 Application Pages
 Custom WebParts
 Etc…

Easy way – Save javascript in txt file, upload
file to SharePoint library, use the content
editor webpart to point to txt file
<div id="demoHtmlArea"></div>
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getTheList, "sp.js");
function getTheList() {
// Get the current client context to start. Most things run from the client context.
clientContext = new SP.ClientContext.get_current();
var myList = clientContext.get_web().get_lists().getByTitle('Announcements');
// use a standard syntax CAML query to filter your results and the fields returned if required
var query = new SP.CamlQuery();
var strQuery = "<View><Query><Where><Eq><FieldRef Name='Show'/><Value Type='bit'>1</Value></Eq></Where></Query></View>";
query.ViewXml = strQuery;
// add your query to the getItems command for your list
this.collListItems = myList.getItems(query);
// issue the load command, these can be batched for multiple operations
clientContext.load(collListItems);
// execute the actual query against the server, and pass the objects we have created to either a success or failure function
clientContext.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction), Function.createDelegate(this, this.myFailFunction));
}
function mySuccessFunction(sender, args) {
// Do something useful like loop through your returned list items and output them somewhere
// create an enumerator to loop through the list with
var listItemEnumerator = this.collListItems.getEnumerator();
var htmlTable = "<table border='1'><tr><th>Title</th><th>Body</th><th>Show</th></tr>";
// loop through the list items
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var Title = oListItem.get_item('Title');
var Body = oListItem.get_item('Body');
var Show = oListItem.get_item('Show');
if(Show)
htmlTable += "<tr><td>" + Title + "</td><td>" + Body + "</td><td>" + Show + "</td></tr>";
}
htmlTable += "</table>";
$("#demoHtmlArea").append(htmlTable); //jquery to insert into the html (regular js can be used too)
}
function myFailFunction(sender, args) {
// do something to alert the user as to the error here.
}
</script>


Out of the box WebPart with SharePoint
Multiple configurations to accomplish the
Goal of Querying
Query and Filter
Group and Sort
Use Predefined Styles and set fields




Custom XSLT
Found in Style Library – XSL Style Sheets –
ItemStyle.xsl
Add xslt node to create new item style
HeaderStyle.xsl contains header styles
<xsl:template name="CustomDemo" match="Row[@Style='CustomDemo']" mode="itemstyle">
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="DisplayTitle">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="Title" select="@Title"/>
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
</xsl:call-template>
</xsl:variable>
<div >
<xsl:value-of select="$DisplayTitle"/> -<xsl:value-of select="@Description" /> -<xsl:value-of select="@Show" />
</div>
</xsl:template>

Sometimes you need to build a production
ready, reusable content query webpart with the
following
 Custom xsl
 Custom header xsl
 Custom item xsl
 Deployable – through SharePoint solution
 Feature Activated
 Reusable

Blog: www.greggalipeau.com
Twitter: @ggalipeau
LinkedIn: http://www.linkedin.com/in/greggalipeau

SharePointShout –http://www.sharepointshout.com

