Tags

, , ,

SharePoint, being organized in a hierarchical structure, requires often from the developer to store information on central (root) level to be used in sub-sites. There is a new trend today to make use of JavaScript for this, but in the same time the old dataformawebpart is still very reliable for this. Personally, I still prefer the old way as this makes from SharePoint development a sort of puzzle solving. The demonstration is made in SharePoint Online environment, but works also in SharePoint 2007, 2010 and 2013 (on premise). As, SharePoint Designer 2013 is buggy when it comes with dataformwebpart, I will explain how to do this directly in the code.

As usual, you need to create dataformwebpart structure which contains following tags: datasources, parameterbindings and xsl.

<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="8" Title="My webpart" PageType="PAGE_DISPLAYFORM"  Default="FALSE" __markuptype="vsattributemarkup" partorder="3" __WebPartId="{E1FDB6EC-29CE-448B-AD4F-066EA2FE7E2E}" id="dataFromRoot">
<DataSources>
</DataSources>
<ParameterBindings>
</ParameterBindings>
<datafields></datafields>
<XSL>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" 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">
	
</xsl:stylesheet>	
</XSL>
</WebPartPages:DataFormWebPart>

Now, inside the datasource tag, you need to add a data source (SPDataSource in our case).

		<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" UseServerDataFormat="True" selectcommand="&lt;View&gt;&lt;/View&gt;" id="MySource">
		<SelectParameters>
		<WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Company News"/>
		<asp:Parameter Name="WebID" DefaultValue="RootWeb" />
		</SelectParameters>
		</SharePoint:SPDataSource>

You can note I am using ListName parameter instead of ListId to make my data source work in other websites if the list name remains the same. Also, WebID parameter, being set to RootWeb, makes my data source to look on root level for my list.

The last thing is to write XSLT logic to be added inside XSL->xsl:stylesheet tag. In our example, it will be a simple iteration through the items.

	<xsl:template match="/">
	<xsl:for-each select="/dsQueryResponse/Rows/Row">
	<xsl:value-of select="@Title" disable-output-escaping="yes" /><br />
	</xsl:for-each>	
	</xsl:template>

Once you tested the functionality, is safe to export the webpart. For this, I usually used SharePoint Designer, which has an option to save it to file.

IMPORTANT: open exported webpart into Visual Studio and remove ListName and ListId properties. If you miss this, dataformwebpart will failed with a message: “List does not exist”.

You can now import the webpart into the site gallery and start to use it. Below is the full code I used for this example. Happy coding!

<WebPartPages:DataFormWebPart runat="server" IsIncluded="True" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="8" Title="My webpart" PageType="PAGE_DISPLAYFORM"  Default="FALSE" __markuptype="vsattributemarkup" partorder="3" __WebPartId="{E1FDB6EC-29CE-448B-AD4F-066EA2FE7E2E}" id="dataFromRoot">
<DataSources>
		<SharePoint:SPDataSource runat="server" DataSourceMode="List" UseInternalName="true" UseServerDataFormat="True" selectcommand="&lt;View&gt;&lt;/View&gt;" id="MySource"><SelectParameters><WebPartPages:DataFormParameter Name="ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="Company News"/><asp:Parameter Name="WebID" DefaultValue="RootWeb" />
		</SelectParameters>
		</SharePoint:SPDataSource>

</DataSources>
<ParameterBindings>
</ParameterBindings>
<datafields></datafields>
<XSL>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" 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">
	<xsl:template match="/">
		<xsl:for-each select="/dsQueryResponse/Rows/Row">
	<xsl:value-of select="@Title" disable-output-escaping="yes" /><br />
	</xsl:for-each>	
	</xsl:template>
</xsl:stylesheet>	
</XSL>
</WebPartPages:DataFormWebPart>
Advertisements