Thursday, 25 June 2009

SPDataSource

There are many ways to retrieve SharePoint content such as SPDataSource, SPQuery
and looping through in SharePoint Object library.

Today we will have a look at the SPDataSource. SPDataSource is an object
datasource which supply information from SharePoint by given parameters.



Here is a sample of SPDataSource syntax.

<SharePointWebControls:SPDataSource runat="server" DataSourceMode="List"
UseInternalName="true" selectcommand="<View/>" id="SPDataSource1">

<SelectParameters>

<webpartpages:DataFormParameter Name="ListName" ParameterKey="ListName"
PropertyName="ParameterValues" DefaultValue="Tasks"/>

</SelectParameters>


</SharePointWebControls:SPDataSource>



DataSourceMode:
(

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatasource.datasourcemode.aspx

)

List: Retrieves
data from a list


<SharePoint:SPDataSource ID="SPDataSource1"
runat="server" DataSourceMode="List" UseInternalName="true"
SelectCommand="<Query><OrderBy><FieldRef Name='DueDate' Ascending='true'
/></OrderBy></Query>">

<SelectParameters>

<asp:Parameter
Name="ListName" DefaultValue="Tasks" />

<asp:Parameter
Name="WebID" DefaultValue="RootWeb" />

</SelectParameters>

</SharePoint:SPDataSource>




<asp:GridView ID="GridView1"
runat="server" DataSourceID="SPDataSource1" AutoGenerateColumns="false">

<Columns>

<asp:BoundField HeaderText="Due Date"
DataField="DueDate" />

<asp:BoundField HeaderText="Priority"
DataField="Priority" />

<asp:BoundField HeaderText="Title"
DataField="Title" />

<asp:BoundField HeaderText="Status"
DataField="Status" />

</Columns>

</asp:GridView>



ListItem: Retrieves
data from a single list item


<SharePoint:SPDataSource
ID="SPDataSource1" runat="server" DataSourceMode="ListItem"
UseInternalName="true" >


<SelectParameters>

<asp:Parameter
Name="WebID" DefaultValue="RootWeb" />

<asp:Parameter
Name="ListName" DefaultValue="Announcements" />

<asp:Parameter
Name="ListItemID" DefaultValue="1" />


</SelectParameters>


</SharePoint:SPDataSource>


<asp:GridView
ID="GridView1" runat="server" DataSourceID="SPDataSource1"
AutoGenerateColumns="false">

<Columns>


<asp:BoundField
HeaderText="Title" DataField="Title" />

<asp:BoundField
HeaderText="Expires" DataField="Expires" />

</Columns>

</asp:GridView>



CrossList:
Retrieves data from multiple lists in multiple websites in the same site
collection.



<SharePoint:SPDataSource ID="SPDataSource1" runat="server"
DataSourceMode="CrossList" UseInternalName="true" SelectCommand="<Webs
Scope='Recursive'></Webs> <Lists ServerTemplate='106'></Lists> <View>
<ViewFields> <FieldRef Name='EventDate'/> <FieldRef Name='Title'/> <FieldRef
Name='Location'/ </ViewFields> <Query> <Where> <And> <Eq> <FieldRef
Name='Location'/> <Value Type='Text'>Your office</Value> </Eq> <Eq> <FieldRef
Name='EventDate'/> <Value Type='DateTime'><Today/></Value> </Eq> </And> </Where>
</Query> </View>" >


</SharePoint:SPDataSource>

<asp:GridView
ID="GridView1" runat="server" DataSourceID="SPDataSource1"
AutoGenerateColumns="false" Width="75%" HeaderStyle-HorizontalAlign="Left" >

<Columns>

<asp:BoundField
HeaderText="Date" DataField="EventDate" />

<asp:BoundField
HeaderText="Title" DataField="Title" />

<asp:BoundField
HeaderText="Location" DataField="Location" />

</Columns>

</asp:GridView>



ListOfLists:
Retrieves properties of lists in a specified web site.



<SharePoint:SPDataSource ID="SPDataSource1" runat="server"
DataSourceMode="CrossList" UseInternalName="true" SelectCommand="<Webs
Scope='Recursive'></Webs> <Lists ServerTemplate='106'></Lists> <View>
<ViewFields> <FieldRef Name='EventDate'/> <FieldRef Name='Title'/> <FieldRef
Name='Location'/ </ViewFields> <Query> <Where> <And> <Eq> <FieldRef
Name='Location'/> <Value Type='Text'>Your office</Value> </Eq> <Eq> <FieldRef
Name='EventDate'/> <Value Type='DateTime'><Today/></Value> </Eq> </And> </Where>
</Query> </View>" >


</SharePoint:SPDataSource>

<asp:GridView
ID="GridView1" runat="server" DataSourceID="SPDataSource1"
AutoGenerateColumns="false" Width="75%" HeaderStyle-HorizontalAlign="Left" >

<Columns>


<asp:BoundField
HeaderText="Date" DataField="EventDate" />

<asp:BoundField
HeaderText="Title" DataField="Title" />


<asp:BoundField
HeaderText="Location" DataField="Location" />


</Columns>

</asp:GridView>



Web: Retrieves
properties of subsites of the current web site.



<SharePoint:SPDataSource ID="SPDataSource1" runat="server" DataSourceMode="Webs"
IncludeHidden="true">


<SelectParameters>

<asp:Parameter
Name="WebId" DefaultValue="RootWeb" />


</SelectParameters>


</SharePoint:SPDataSource>

<asp:GridView
ID="GridView1" runat="server" DataSourceID="SPDataSource1"
AutoGenerateColumns="false">

<Columns>

<asp:BoundField
HeaderText="Site Title" DataField="__spTitle" />

<asp:BoundField
HeaderText="URL" DataField="__spUrl" />

<asp:BoundField
HeaderText="ID" DataField="__spID" />

<asp:BoundField
HeaderText="Web Template" DataField="__spWebTemplate" />

<asp:BoundField
HeaderText="Web Template Id" DataField="__spWebTemplateId" />

</Columns>

</asp:GridView>



SelectCommand: This is where we define our
query to get data. CAML is the query syntax. Custom parameters such as
QueryString, existing variables, programmatically defined
parameterbindings can be used.

SelectParameters (

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatasource.selectparameters.aspx

): That is used to define to
pass parameters into data control.

Following list states
what can be assigned as default values




































WebID

”RootWeb” for the root Web site. Otherwise, a string representation of a GUID
such as the value of the SPWeb.ID property.

WebURL

An empty string for the root Web site. Otherwise, a string containing a
server-relative URL.

ListID

A string representation of a GUID such as the value of the SPList.ID property.

ListName

The value of the SPList.Title property.

ListItemGUID

A A string representation of a GUID such as the value of the SPListItem.UniqueId
property.

ListItemID

A string representation of an integer such as the value of the SPListItem.ID
property.

RootFolder

The value of the SPFolder.Name property.

FolderID

A string representation of a GUID such as the value of the SPFolder.UniqueId
property.

Additionally we can assign parameters from;

QueryString: <asp:QueryStringParameter Name="CompanyName"
QueryStringField="Company" DefaultValue="Contoso" />


Control: <asp:ControlParameter Name="CompanyName" ControlID="DropDownList1"
PropertyName="SelectedValue" />

ParameterBinding: <webpartpages:DataFormParameter Name="PostID"
ParameterKey="PostID" PropertyName="ParameterValues" DefaultValue="0"/>

Wednesday, 24 June 2009

DataFormWebPart

One of the most important part in SharePoint is displaying the content without
braking any SharePoint features.

In this scenario DataFormWebPart is our guidance. There are always other ways to
display the content like custom webparts/webpartpages, but developing with these
methods may cost us lots of time.

In the following example we will learn how to develop a simple DataFormWebPart.


This is the syntax we will work on it.

<WebPartPages:DataFormWebPart id="TasksDisplayDataFormWebPart" runat="server"
FrameType="None" NoDefaultStyle="TRUE" ViewFlag="1" Title="Tasks"
DisplayName="Tasks" __markuptype="vsattributemarkup"
__WebPartId="{418745AA-67C6-481e-95F9-5C824AD0CE61}" WebPart="true">

<DataSources>

<SharePointWebControls:SPDataSource runat="server" DataSourceMode="List"
UseInternalName="true" selectcommand="<View/>" id="SPDataSource1">

<SelectParameters>

<webpartpages:DataFormParameter Name="ListName" ParameterKey="ListName"
PropertyName="ParameterValues" DefaultValue="Tasks"/>

</SelectParameters>


</SharePointWebControls:SPDataSource>

</DataSources>

<ParameterBindings>

<ParameterBinding Name="ListName" Location="None" DefaultValue="Tasks" />

<ParameterBinding Name="dvt_apos" Location="Postback;Connection" />

<ParameterBinding Name="UserID" Location="CAMLVariable"
DefaultValue="CurrentUserName" />

<ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate" />

<ParameterBinding Name="WebPartTitle" Location="None" DefaultValue="{Title}" />

<ParameterBinding Name="WebPartTitleURL" Location="None" DefaultValue="{TitleUrl}" />

</ParameterBindings>

<datafields>@Title,Title;@ID,ID</datafields>

<XslLink>/Style Library/XSL Style Sheets/SPTest/FullContent.xsl</XslLink>

</WebPartPages:DataFormWebPart >



Now let’s have a look at the properties;


a) DataSources: This defines where the data to get from We have some controls to
get the data, here is some of them;




i) SPDataSource: Gets the data from single or multiple (cross, we
will mention it in later blogs) SharePoint libraries content.


DataSourceMode:


• List: Retrieves
data from a list


• ListItem: Retrieves
data from a single list item


• CrossList:
Retrieves data from multiple lists in multiple websites in the same site
collection.


• ListOfLists:
Retrieves properties of lists in a specified web site.


• Web: Retrieves
properties of subsites of the current web site.


SelectCommand: This is where we define our
query to get data. CAML is the query syntax. Custom parameters such as
QueryString, existing variables, programmatically defined
parameterbindings can be used, but it will be shown on feature blogs.


SelectParameters: That is used to define to
pass parameter binding values and also other type of parameters.

ii) SPXmlDataSource:


<SharePoint:SPXmlDataSource runat="server">

<DataFileParameters>


<WebPartPages:DataFormParameter Name="FileName" ParameterKey="FileName"
PropertyName="ParameterValues" DefaultValue="items.xml"/>


<WebPartPages:DataFormParameter Name="FilePath" ParameterKey="FilePath"
PropertyName="ParameterValues" DefaultValue=""/>

</DataFileParameters>

</SharePoint:SPXmlDataSource>




b) ParameterBindings: To supply user defined or environment defined values to
the actual query.

c) DataFields: Fields those are used in xsl.

d) XSLLink: Xsl file to display content in a custom format.

Sample of the following code xsl;

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0"
exclude-result-prefixes="xsl msxsl ddwrt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:SharePoint="Microsoft.SharePoint.WebControls"
xmlns:ddwrt2="urn:frontpage:internal">

<xsl:output method="html" indent="no"/> <xsl:decimal-format NaN=""/>

<xsl:param name="dvt_apos">'</xsl:param>

<xsl:param name="WebPartTitle">

</xsl:param>

<xsl:param name="WebPartTitleURL"></xsl:param> <xsl:variable
name="dvt_1_automode">0</xsl:variable>

<xsl:template match="/">

<xsl:call-template name="dvt_1"/> </xsl:template>

<xsl:template name="dvt_1">

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>

<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">

</xsl:if>

<xsl:call-template name="dvt_1.body">

<xsl:with-param name="Rows" select="$Rows"/>

</xsl:call-template>

</xsl:template>

<xsl:template name="dvt_1.body">

<xsl:param name="Rows"/>

<xsl:for-each select="$Rows">

<xsl:call-template name="dvt_1.rowview"/>

</xsl:for-each>

</xsl:template>

<xsl:template name="dvt_1.rowview"> Title: <xsl:value-of select="@Title"/><br/>

</xsl:template>

</xsl:stylesheet>