XSLTListViewWebpart property XMLDefinition does work

31 Mar

Adding a list as web part to a page results in an XSLTListViewWebpart.

1-xsltlistviewwebpart-initial-view

Figure 1 Added a list to a page

This results in a hidden view configured with the query and field references used. The accompanying view xml is shown in figure 3. Figure 2 displays the number of views before adding the web part to the page.

2-xsltlistviewwebpart-initial-listviews

Figure 2 Listviews before adding the list to a page

3-xsltlistviewwebpart-hidden-view-xml

Figure 3 Hidden view configuration

Configuring the fields shown in the web part results in a replaced hidden view.
The view configured:

4-xsltlistviewwebpart-configured-view

Figure 4 Configured view

And the accompanying xml shown in Figure 5.

5-xsltlistviewwebpart-hidden-view-xml-new

Figure 5 Hidden view xml

When this web part is exported to a web part file the view configuration is stored in an xml property called XmlDefinition.

<property name="XmlDefinition" type="string">
&lt;View Name="{4A2EFF1A-3B9D-4666-BED2-746669E3F037}" MobileView="TRUE" Type="HTML"
Hidden="TRUE" DisplayName="" Url="/teams/testteamsite/TestDefaultValue/SitePages/Home.aspx"
Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/dlicon.png?rev=44" &gt;&lt;
Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name="FileLeafRef"/&gt;&lt;/OrderBy&gt;&lt;/Query&gt;&lt;
ViewFields&gt;&lt;FieldRef Name="DocIcon"/&gt;&lt;FieldRef Name="LinkFilename"/&gt;&lt;
FieldRef Name="Modified"/&gt;&lt;FieldRef Name="_UIVersionString"/&gt;&lt;/
ViewFields&gt;&lt;RowLimit Paged="TRUE"&gt;30&lt;/RowLimit&gt;&lt;Aggregations Value="Off"/&gt;&lt;
JSLink&gt;clienttemplates.js&lt;/JSLink&gt;&lt;XslLink Default="TRUE"&gt;main.xsl&lt;/XslLink&gt;&lt;
Toolbar Type="Standard"/&gt;&lt;/View&gt;
</property>

Importing this web part through the UI or using SharePoint PnP (which uses CSOM in their modules) in the same web on another page results in a fully configured web part as exported and as expected, shown in figure 4.
SharePoint PnP is used here because the provisioning of pages has to be done most of the time in projects in a repeatable way.

Use in other web

Specific list references have to be removed (ListId property) or changed (ListUrl and ListName properties) when importing this web part in another web.
After modifying the properties of the web part file and import it on another web where the list specified in the xml exists, results in a default view as in Figure 1 using SharePoint PnP.

The XmlDefinition property seems to be ignored, because the Version column isn’t visible.

6-xsltlistviewwebpart-xmldefinition-seems-to-be-ignored

Figure 6 XmlDefinition seens to be ignored, Version column not visible

It seems to be ignored, because the exported data in the property isn’t valid.

When there’s xml in a CDATA tag like shown in the xml snippet below, the configuration will be applied.


<webParts>
<webPart xmlns="<a href="http://schemas.microsoft.com/WebPart/v3">http://schemas.microsoft.com/WebPart/v3</a>">
<metaData>
<type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0,
  Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">Test</property>
<property name="ListName" type="string">DemoDocs</property>
<property name="ListUrl" type="string">DemoDocs</property>
<property name="XmlDefinition" type="string"><![CDATA[
<View Type="HTML" Name="DoesntMatter" BaseViewID="1">
<Query>
<GroupBy Collapse="TRUE" GroupLimit="30">
<FieldRef Name="Author"/>
</GroupBy>
<OrderBy>
<FieldRef Name="Created" Ascending="FALSE"/>
<FieldRef Name="Title"/>
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name="ID"/>
<FieldRef Name="DocIcon"/>
<FieldRef Name="LinkFilename"/>
<FieldRef Name="Editor"/>
<FieldRef Name="Created"/>
<FieldRef Name="FileSizeDisplay"/>
</ViewFields>
<RowLimit Paged="TRUE">9</RowLimit>
<Aggregations Value="On">
<FieldRef Name="DocIcon" Type="COUNT"/>
</Aggregations>
<JSLink>clienttemplates.js</JSLink>
<Toolbar Type="Standard"/>
</View>]]></property>
</properties>
</data>
</webPart>
</webParts>

The result at the page:

7-xsltlistviewwebpart-configured-view-at-other-site

Figure 7 Configured view at other site

Even the Toolbar set to None works:

8-xsltlistviewwebpart-configured-view-at-other-site-even-toolbar-none-works

Figure 8: Toolbar None works as expected

 

9-xsltlistviewwebpart-view-xml-at-other-site-even-toolbar-none-works

Figure 9 Configured view xml

Summary

When an XSLTListViewWebpart is exported the XmlDefinition property contains xml which can’t be used when the webpart is imported in another site using client side object model. Use the CDATA tag as shown en it all works.

3 Replies to “XSLTListViewWebpart property XMLDefinition does work

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.