Thursday, December 6, 2012

SPDataSource

Example:
1.  <SharePoint:SPDataSource runat="server"
2.  DataSourceMode="List"
3.  SelectCommand="<View></View>"
4.  id="dataviewdatasource1" >
5.  <SelectParameters>
6.  <WebPartPages:DataFormParameter Name="ListID"
7.  ParameterKey="ListID"
8.  PropertyName="ParameterValues"
9.  DefaultValue="00000000-0000-0000-0000-000000000000" />
10. <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
11. </SelectParameters>
12. </SharePoint:SPDataSource>

DataSourceMode

Data Source Mode is a type of WSS data to query. The valid modes are:
  • List - Query items in a single list. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPQuery class against a list. This mode can get lookup fields. This is the default mode.
  • ListOfLists - Query property values of all lists in a Web site. Multiple rows of data are returned. Each row represents a list.
  • CrossList - Query items in multiple lists in multiple Web sites in the same site collection. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPSiteDataQuery class against lists in a site collection. This mode cannot get lookup fields. used for recursive query.
  • Webs - Query property values of all sub sites of a specified Web site. Multiple rows of data are returned. Each row represents a Web site.
  • ListItem - Query field values in a list item. A single row of list data is returned. The row represents a single list item.

SelectCommand

  • Select Command is an actual query for that data type.
  • Some modes don’t require a query at all.
  • In List mode, the SelectCommand roughly maps to the SPQuery object and accepts Collaborative Application Markup Language (CAML) fragments to specify things like the set of return fields, filtering, and sort order.
  • Briefly, this command will draw the wanted fields we want to draw from the data source to the DVWP. For example, if we want to draw the field “Title”, the code will be: SelectCommand="<View><ViewFields><FieldRef Name=’Title’/></ViewFields></View>"
  • But, if we want to create a query, the code will be written in CAML.SelectParameter
  • Select Parameter is each command expects a number of different parameters.
  • These parameters can refer to the parameter binding’s collection in the Data View, as well as take most ASP.Net types of parameters like Control and QueryString.

Where from? <asp:Parameter DefaultValue="…" Name="WebUrl"></asp:Parameter>

  • {sitecollectionroot} – will get the data from the root site: <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
  • If we want a specify sub-site: <asp:Parameter DefaultValue="/sub-site" Name="WebUrl"></asp:Parameter>

Parameteres

List and ListItem modes require the “DataFormParameter” list ID or list name, for example (CrossList for example need that parameter but not using it):
<WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="00000000-0000-0000-0000-000000000000" />
<WebPartPages:DataFormParameter Name=" ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="<List Name>" />
  • ListID – GUID that corresponds to a List’s ListID.
  • ListName – Display name for a List.
  • ListItemId – ID for a single item in ListItem mode
  • WebUrl – Url to the web. When not specified, SPDataSource uses the current web to resolve the previous parameters. You can also use this parameter to access lists in other Web sites in the same Site Collection assuming you have permissions to that list.

- Thanks to Eray Chou' post.