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"/>

1 comment:

  1. I am having ListA lookup reference to ListB which in turn looks up in ListC. I am having a CAML Join Query construct which returns FieldA1, FieldB1 and FieldC using lookup references. The same CAML Join query is working when used programatically using SPQuery object. But need this output via SPDataSource so that I can bind it to DFWP. can we use CAML Join Queries in "SelectCommand"? I tried it but could not succeed.

    ReplyDelete