A Tour of SharePoint Data Source Controls

Introduction

Besides the classic ASP.NET data source controls, SharePoint brings along its own. These allow us to retrieve data not just from SharePoint, but also from external sources, such as web services or SQL databases. Using SharePoint Designer you end up using these data sources without even knowing, and, specially, ignoring what other options you have. Let’s look at these controls one by one and see how the can be used. I already talked about the SPDataSource and AggregateDataSource, so I won’t cover these here. I also won’t be covering them in depth, but instead will provide an example of how to use them.

SPHierarchyDataSourceControl

The SPHierarchyDataSourceControl allows us to retrieve data (documents, list items) hierarchically from either a site or a list. It can only be bound to a control that can display hierarchical data, such as the SPTreeView control. It cannot be easily customized, except by using CSS and JavaScript.

A simple example is:

   1: <SharePoint:SPHierarchyDataSourceControl runat="server" ID="hierarchy" RootContextObject="Web" ShowDocLibChildren="true" ShowListChildren="true" ShowFolderChildren="true" ShowWebChildren="true"/>
   2: <SharePoint:SPTreeView runat="server" DataSourceID="hierarchy"/>

BdcDataSource

A BdcDataSource let’s you retrieve data from a BCS data source (external content type). This may happen when you don’t have an external list created or you want to call a specific finder method. You need to specify the namespace, LOB instance, entity, and finder method. If the finder needs parameters, you will need to supply them. It can be customized by hosting it in a DataFormWebPart and by applying XSLT.

Here’s an example:

   1: <WebPartPages:DataFormWebPart runat="server" Title="BCS" DisplayName="BCS" ID="bcs">
   2:     <DataSources>
   3:         <SharePoint:BdcDataSource runat="server" Mode="List" EntityName="Parent" LobSystemInstanceName="BCS" EntityNamespace="http://sp2013" FinderName="Read List"/>
   4:     </DataSources>
   5:     <Xsl>
   6:         <xsl:stylesheet 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">
   7:             <xsl:output method="html" indent="no"/>
   8:  
   9:                 <xsl:template match="/">
  10:                     <xsl:variable name="Rows" select="/dsQueryResponse/NewDataSet/Row"/>
  11:                     Count: <xsl:value-of select="count($Rows)"/>
  12:                 </xsl:template>
  13:             
  14:         </xsl:stylesheet>
  15:     </Xsl>
  16: </WebPartPages:DataFormWebPart>

SoapDataSource

SoapDataSource is the control that retrieves data from a SOAP web service. You specify the URL of the web service, the action to call and the SOAP envelope, together with any required parameters. It should be hosted in a DataFormWebPart and can thus use XSLT for the formatting of its contents.

An example of calling the Lists.asmx web service:

   1: <WebPartPages:DataFormWebPart runat="server" DisplayName="SOAP" Title="SOAP" ID="soap">
   2:     <DataSources>
   3:         <SharePoint:SoapDataSource runat="server" WsdlPath="http://sp2013/_vti_bin/lists.asmx?WSDL" SelectUrl="http://sp2013/_vti_bin/lists.asmx" SelectAction="http://schemas.microsoft.com/sharepoint/soap/GetListCollection" SelectPort="ListsSoap" SelectServiceName="Lists">
   4:             <SelectCommand>
   5:                 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   6:                     <soap:Body>
   7:                         <GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/"/>
   8:                     </soap:Body>
   9:                 </soap:Envelope>
  10:             </SelectCommand>
  11:         </SharePoint:SoapDataSource>
  12:     </DataSources>
  13:     <XSL>
  14:         <xsl:stylesheet xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ddw1="http://schemas.microsoft.com/sharepoint/soap/" 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">
  15:             <xsl:output method="html" indent="no"/>
  16:     
  17:             <xsl:template match="/">
  18:                 <xsl:variable name="Rows" select="/soap:Envelope/soap:Body/ddw1:GetListCollectionResponse/ddw1:GetListCollectionResult/ddw1:Lists/ddw1:List"/>
  19:                 Count: <xsl:value-of select="count($Rows)"/>
  20:             </xsl:template>
  21:         </xsl:stylesheet>
  22:     </XSL>
  23: </WebPartPages:DataFormWebPart>

XsltListViewWebPart (External List)

External lists can be displayed using the XsltListViewWebPart. Nothing really new here.

A simple example (of course, do replace the list and view GUIDs):

   1: <WebPartPages:XsltListViewWebPart runat="server" Title="External List" DisplayName="External List" ListName="{C9DD692D-27C6-4B99-A197-F8892F293C04}" ID="external">
   2:     <XmlDefinition>
   3:         <View Name="{DDD613DD-BA38-4DBF-9A89-5502B0152EE1}" Type="HTML" DisplayName="Read List" Url="/Lists/ExternalList/Read List.aspx" BaseViewID="1">
   4:             <Method Name="Read List"/>
   5:         </View>
   6:     </XmlDefinition>
   7:     <Xsl>
   8:         <xsl:stylesheet 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">
   9:             <xsl:output method="html" indent="no"/>
  10:  
  11:                 <xsl:template match="/dsQueryResponse">
  12:                     <xsl:variable name="Rows" select="Rows/Row"/>
  13:                     Count: <xsl:value-of select="count($Rows)"/>    
  14:                 </xsl:template>
  15:         </xsl:stylesheet>
  16:     </Xsl>
  17: </WebPartPages:XsltListViewWebPart>

XmlUrlDataSource

The XmlUrlDataSource is used for invoking REST web services. Similar to  SoapDataSource, you need to pass it the URL, but also the HTTP method and any parameters. I is also usually hosted in a DataFormWebPart.

Here’s an example of calling the weather service I talked about in another post:

   1: <WebPartPages:DataFormWebPart runat="server" Title="REST" DisplayName="REST" ID="rest">
   2:     <DataSources>
   3:         <SharePoint:XmlUrlDataSource runat="server" AuthType="None" HttpMethod="GET" SelectCommand="http://api.openweathermap.org/data/2.5/weather">
   4:             <SelectParameters>
   5:                 <WebPartPages:DataFormParameter Name="id" ParameterKey="id" PropertyName="ParameterValues" DefaultValue="2740637"/>
   6:                 <WebPartPages:DataFormParameter Name="mode" ParameterKey="mode" PropertyName="ParameterValues" DefaultValue="xml"/>
   7:                 <WebPartPages:DataFormParameter Name="units" ParameterKey="units" PropertyName="ParameterValues" DefaultValue="metric"/>
   8:             </SelectParameters>
   9:         </SharePoint:XmlUrlDataSource>
  10:     </DataSources>
  11:     <Xsl>
  12:         <xsl:stylesheet 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">
  13:             <xsl:output method="html" indent="no"/>
  14:  
  15:                 <xsl:template match="/">
  16:                     <xsl:variable name="Rows" select="/current"/>
  17:                     Count: <xsl:value-of select="count($Rows)"/>
  18:                 </xsl:template>
  19:             
  20:         </xsl:stylesheet>
  21:     </Xsl>
  22: </WebPartPages:DataFormWebPart>

SPSqlDataSource

The SPSqlDataSource is what SharePoint uses to access a SQL database. If you use SharePoint Designer to add one such data source, you might be surprised to see that it places a SqlDataSource instead. The thing is, SharePoint uses ASP.NET tag mapping to replace any SqlDataSource control for a SPSqlDataSource. Again, we host it inside a DataFormWebPart for easily formatting its contents and you can pass parameters to the SQL.

An example:

   1: <WebPartPages:DataFormWebPart runat="server" Title="SQL" DisplayName="SQL" ID="sql">
   2:     <DataSources>
   3:         <SharePoint:SPSqlDataSource runat="server" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=servername;User ID=username;Password=password;Initial Catalog=database;" SelectCommand="SELECT * FROM [SomeTable]"/>
   4:     </DataSources>
   5:     <Xsl>
   6:         <xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" 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">
   7:             <xsl:output method="html" indent="no"/>
   8:  
   9:                 <xsl:template match="/">
  10:                     <xsl:variable name="Rows" select="/dsQueryResponse/NewDataSet/Row"/>
  11:                     Count: <xsl:value-of select="count($Rows)"/>
  12:                 </xsl:template>
  13:         </xsl:stylesheet>
  14:     </Xsl>
  15: </WebPartPages:DataFormWebPart>

SPCalendarDataSource

SPCalendarDataSource cannot be used exclusively on the markup, it needs a List property that can only be set through code. It is usually used to populate a SPCalendarView control.

An example of the markup:

   1: <SharePoint:SPCalendarDataSource runat="server" ID="calendar" ViewType="Week"/>
   2: <SharePoint:SPCalendarView runat="server" DataSourceID="calendar"/>

Conclusion

While SPDataSource is generally the most generic and useful control, it cannot do everything, namely, access external sources. For that, we have other options; the AggregateDataSource can be used to bring together data from all of these sources, except SPHierarchyDataSourceControl and SPCalendarDataSource, but you can easily replace these by SPDataSource. I did not mention SPWorkflowDataSource because it isn't a general-purpose data source control, it is merely used to set or get workflow association parameters.

Let me know if you have any questions!

Update: I have a post on PortalSiteMapDataSource here.

                             

3 Comments

  • Great article… Thanks for your great information, the contents are quiet interesting. Let be explain more on SharePoint data source controls.

  • I have a project built in SharePoint 2013. I have a SPDataSource as Base class in my project. I have a reference to the Microsoft.SharePoint.dll file but still this SPDataSource and SPDataSourceView are giving me error.

    Missing Namespace error

    Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'SPDataSource' could not be found (are you missing a using directive or an assembly reference?)

  • Sigar: try to reference it using the full name, Microsoft.SharePoint.WebControls.SPDataSource.

Add a Comment

As it will appear on the website

Not displayed

Your website