Adding a list as web part to a page results in an XSLTListViewWebpart.
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.
Figure 2 Listviews before adding the list to a page
Figure 3 Hidden view configuration
Configuring the fields shown in the web part results in a replaced hidden view.
The view configured:
Figure 4 Configured view
And the accompanying xml shown in Figure 5.
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"> <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" >< Query><OrderBy><FieldRef Name="FileLeafRef"/></OrderBy></Query>< ViewFields><FieldRef Name="DocIcon"/><FieldRef Name="LinkFilename"/>< FieldRef Name="Modified"/><FieldRef Name="_UIVersionString"/></ ViewFields><RowLimit Paged="TRUE">30</RowLimit><Aggregations Value="Off"/>< JSLink>clienttemplates.js</JSLink><XslLink Default="TRUE">main.xsl</XslLink>< Toolbar Type="Standard"/></View> </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.
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:
Figure 7 Configured view at other site
Even the Toolbar set to None works:
Figure 8: Toolbar None works as expected
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.
Really great stuff – but how to remove the “Checkbox” (tabular view)
Thank you so much. I had been struggling get this working and this approach worked.
hi
have You ever done this with O365?
I get exception “invald xml” in XmlDefinition
Thanks
zli