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 talkedabout 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:SPHierarchyDataSourceControlrunat="server"ID="hierarchy"RootContextObject="Web"ShowDocLibChildren="true"ShowListChildren="true"ShowFolderChildren="true"ShowWebChildren="true"/>
2:<SharePoint:SPTreeViewrunat="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:DataFormWebPartrunat="server"Title="BCS"DisplayName="BCS"ID="bcs">
2:<DataSources>
3:<SharePoint:BdcDataSourcerunat="server"Mode="List"EntityName="Parent"LobSystemInstanceName="BCS"EntityNamespace="http://sp2013"FinderName="Read List"/>
4:</DataSources>
5:<Xsl>
6:<xsl:stylesheetversion="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:outputmethod="html"indent="no"/>
8: 9:<xsl:templatematch="/">
10:<xsl:variablename="Rows"select="/dsQueryResponse/NewDataSet/Row"/>
11: Count: <xsl:value-ofselect="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:DataFormWebPartrunat="server"DisplayName="SOAP"Title="SOAP"ID="soap">
2:<DataSources>
3:<SharePoint:SoapDataSourcerunat="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:Envelopexmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
6:<soap:Body>
7:<GetListCollectionxmlns="http://schemas.microsoft.com/sharepoint/soap/"/>
8:</soap:Body>
9:</soap:Envelope>
10:</SelectCommand>
11:</SharePoint:SoapDataSource>
12:</DataSources>
13:<XSL>
14:<xsl:stylesheetxmlns: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:outputmethod="html"indent="no"/>
16:17:<xsl:templatematch="/">
18:<xsl:variablename="Rows"select="/soap:Envelope/soap:Body/ddw1:GetListCollectionResponse/ddw1:GetListCollectionResult/ddw1:Lists/ddw1:List"/>
19: Count: <xsl:value-ofselect="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:XsltListViewWebPartrunat="server"Title="External List"DisplayName="External List"ListName="{C9DD692D-27C6-4B99-A197-F8892F293C04}"ID="external">
2:<XmlDefinition>
3:<ViewName="{DDD613DD-BA38-4DBF-9A89-5502B0152EE1}"Type="HTML"DisplayName="Read List"Url="/Lists/ExternalList/Read List.aspx"BaseViewID="1">
4:<MethodName="Read List"/>
5:</View>
6:</XmlDefinition>
7:<Xsl>
8:<xsl:stylesheetversion="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:outputmethod="html"indent="no"/>
10: 11:<xsl:templatematch="/dsQueryResponse">
12:<xsl:variablename="Rows"select="Rows/Row"/>
13: Count: <xsl:value-ofselect="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:DataFormWebPartrunat="server"Title="REST"DisplayName="REST"ID="rest">
2:<DataSources>
3:<SharePoint:XmlUrlDataSourcerunat="server"AuthType="None"HttpMethod="GET"SelectCommand="http://api.openweathermap.org/data/2.5/weather">
4:<SelectParameters>
5:<WebPartPages:DataFormParameterName="id"ParameterKey="id"PropertyName="ParameterValues"DefaultValue="2740637"/>
6:<WebPartPages:DataFormParameterName="mode"ParameterKey="mode"PropertyName="ParameterValues"DefaultValue="xml"/>
7:<WebPartPages:DataFormParameterName="units"ParameterKey="units"PropertyName="ParameterValues"DefaultValue="metric"/>
8:</SelectParameters>
9:</SharePoint:XmlUrlDataSource>
10:</DataSources>
11:<Xsl>
12:<xsl:stylesheetversion="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:outputmethod="html"indent="no"/>
14: 15:<xsl:templatematch="/">
16:<xsl:variablename="Rows"select="/current"/>
17: Count: <xsl:value-ofselect="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:DataFormWebPartrunat="server"Title="SQL"DisplayName="SQL"ID="sql">
2:<DataSources>
3:<SharePoint:SPSqlDataSourcerunat="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:stylesheetxmlns: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:outputmethod="html"indent="no"/>
8: 9:<xsl:templatematch="/">
10:<xsl:variablename="Rows"select="/dsQueryResponse/NewDataSet/Row"/>
11: Count: <xsl:value-ofselect="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:SPCalendarDataSourcerunat="server"ID="calendar"ViewType="Week"/>
2:<SharePoint:SPCalendarViewrunat="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.
Let me know if you have any questions!