Accessing List Items using SPDataSource using the object model


Table of Contents

SPDataSource is a control that can be used to access data without needing declarative code. This is handy when you want to bind controls to data that is available in the SharePoint object model.

The example code shows how the SPDataSource is defined using CAML query for the Select Command and passing in various parameters.

<SPWebControls:SPDataSource runat="server" ID="dsPersonTitles" DataSourceMode="List"
  SelectCommand="<Query><OrderBy><FieldRef Name='SortOrder' Ascending='true' /></OrderBy></Query>"
  <SelectParameters>
      <asp:Parameter Name="WebUrl" DefaultValue="/configuration/" />
      <asp:Parameter Name="ListName" DefaultValue="PersonTitles" />
  </SelectParameters>
</SPWebControls:SPDataSource>

The below code shows how the DropDownList control can use this DataSource with the ID 'dsPersonTitles' and map the DataTextField and DataValueField like you would normally do in declarative code.

<asp:DropDownList runat="server" ID="ddlPersonTitles" CssClass="title" DataSourceID="dsPersonTitles" DataTextField="Title" DataValueField="ID">
</asp:DropDownList>

Parameters

The SPDataSource Inherits from SPDataSourceView, basically a helper class for SPDataSourceView.
After a little Reflector-ing of SPDataSourceView I found these keys it is processing from from the parameter values collection.

  • webid
  • weburl
  • rootfolder
  • listid
  • listname
  • listitemguid
  • listitemid
  • folderid
  • nextpagedata
  • startrowindex
  • maximumrows

It uses these parameters inconjuction with the View and dData Fields and the the view to exectue the SQL Select.
Source: BruceVB1

Troubleshooting SPDataSource

Often you will export a SPDataSource to another location and it will web part will throw an error like:

Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator.

The best place to start with this error is to go straight to the 12Hive Logs (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS).
In the example below the SPDataSource is part of a DataFormWebPart and the error found was:

04/02/2009 09:35:42.31 	w3wp.exe (0x17F0)                       	0x1160	Windows SharePoint Services   	Web Controls                  	88wy	Medium  	SPDataSourceView.ExecuteSelect() - selectArguments: IsEmpty=True, MaximumRows=0, RetrieveTotalRowCount=False, SortExpression=, StartRowIndex=0, TotalRowCount=-1
04/02/2009 09:35:42.33 	w3wp.exe (0x17F0)                       	0x1160	Windows SharePoint Services   	Web Parts                     	89a1	Monitorable	Error while executing web part: System.InvalidOperationException: Unable to open the specified list. ---> System.ArgumentException: Value does not fall within the expected range.     at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException)     at Microsoft.SharePoint.SPListCollection.get_Item(String strListName)     at Microsoft.SharePoint.WebControls.SPDataSourceView.GetList(SPContext context, SPWeb web, Guid listId, String listName)     --- End of inner exception stack trace ---     at Microsoft.SharePoint.WebControls.SPDataSourceView.GetList(SPContext context, SPWeb web, Guid listId, String listName)     at Microsoft.SharePoint.WebControls.SPDataSourceView.ExecuteSelect(DataSourceSelectArguments selectArguments)     at System.Web.UI.DataSourceV...
04/02/2009 09:35:42.33*	w3wp.exe (0x17F0)                       	0x1160	Windows SharePoint Services   	Web Parts                     	89a1	Monitorable	...iew.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)     at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigatorInternal()     at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator()     at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator(IDataSource datasource, Boolean originalData)     at Microsoft.SharePoint.WebPartPages.DataFormWebPart.GetXPathNavigator(String viewPath)     at Microsoft.SharePoint.WebPartPages.DataFormWebPart.PrepareAndPerformTransform()
04/02/2009 09:35:42.33 	w3wp.exe (0x17F0)                       	0x1160	Windows SharePoint Services   	Web Parts                     	89a2	Monitorable	InnerException 1: System.ArgumentException: Value does not fall within the expected range.     at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException)     at Microsoft.SharePoint.SPListCollection.get_Item(String strListName)     at Microsoft.SharePoint.WebControls.SPDataSourceView.GetList(SPContext context, SPWeb web, Guid listId, String listName)

The biggest issue here is that the error doesn't really tell you what it is. You can see 'Unable to open the specified list', but in actual fact the issue with the SPDataSource was that a column was being referenced that didn't exist. So be careful when troubleshooting!

External References

There is not much on the MSDN Page. For more information, Chris O'Brien and Keith Dahlby have some great content on the SPDataSource.

Labels

spdatasource spdatasource Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.


  1. Feb 25, 2009

    Anonymous says:

    This is exactly what I was looking for! Thanks mate Pofo =P

    This is exactly what I was looking for! Thanks mate

    Pofo =P

  2. Mar 27, 2009

    David Bullock says:

    http://blogs.msdn.com/sharepointdesigner/archive/2007/04/24/spdatasource-and-rol...
  3. Apr 01, 2009

    Anonymous says:

    I would like to know where this code actually goes. I understand what it does, b...

    I would like to know where this code actually goes. I understand what it does, but I seem to be missing where you can actually put it. I thought it would be able to be added to custom NewForm.aspx page, but this does not seem to work because I get a page not found error after adding this type of code. (Page worked before adding code) daniel.r.walker1@navy.mil

    1. Apr 08, 2009

      Anonymous says:

      this site talks about the error not sure if it will help you now. Do you still h...

      this site talks about the error not sure if it will help you now. Do you still have the problem now or you have solve it. System.ArgumentException. Value does not fall within the expected range

      http://connectionstringexamples.com/article.php?story=Sharepoint-error-System-Argument

  4. Aug 02, 2009

    Nick Hadlee says:

    I've added two links to the External References section which go to the MSDN doc...

    I've added two links to the External References section which go to the MSDN documentation for two important properties of the SPDataSource - DataSourceMode and SelectParameter. There is a suprising amount of detail in those articles and it also includes some examples and how the properties should be used. I think this is newish documentation or it has just been previously buried in MSDN.


Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare