How to add a document library XsltListViewWebPart (or list view web part) on page using CSOM with SP2013 when we do not want to use the in-built view with "Name, Modified, Modified By" fields?
Custom lists get imported correctly via CSOM with this xml, and this also works in import via UI directly, but does not work via CSOM for Document Libraries or Linked Lists for example:
<?xml version="1.0" encoding="utf-8" ?> <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <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="ListUrl" type="string">Lists/MyList</property> <property name="XmlDefinition" type="string"><View Name="{ABCDEFGH-ABCD-ABCD-ABCD-ABCDEFGHIJK}" MobileView="TRUE" Type="HTML" Hidden="TRUE" OrderedView="TRUE" DisplayName="" Url="/sites/site/Pages/default.aspx" Level="255" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/links.png?rev=23" ><Query><OrderBy><FieldRef Name="Order" Ascending="TRUE"/></OrderBy></Query><ViewFields><FieldRef Name="DocIcon"/><FieldRef Name="Edit"/><FieldRef Name="URLwMenu"/></ViewFields><RowLimit Paged="TRUE">99</RowLimit><JSLink>clienttemplates.js</JSLink><XslLink Default="TRUE">main.xsl</XslLink><Toolbar Type="Standard"/></View></property> <property name="MissingAssembly" type="string">Cannot import this Web Part.</property> </properties> </data> </webPart> </webParts>
EDIT / Solution:
As Anthony suggested we should first add the XsltListViewWebPart without specifying the view. Adding the web part will automatically create a hidden view in the list and link the web part to it. Updating this view will also update the web part.
Example solution below.
Web part xml:
<?xml version="1.0" encoding="utf-8" ?> <webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <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="ListUrl" type="string">MyLibrary</property> <property name="MissingAssembly" type="string">Cannot import this Web Part.</property> </properties> </data> </webPart> </webParts>
Code:
WebPart importingWebPart = mgr.ImportWebPart(webPartXml).WebPart; // take webPartXml from above WebPartDefinition wpDefinition = mgr.AddWebPart(importingWebPart, "Top", 1); mgr.Context.Load(wpDefinition, d => d.Id); // Id of the hidden view which gets automatically created mgr.Context.ExecuteQuery(); var viewId = wpDefinition.Id; List list = web.Lists.GetByTitle("MyLibrary"); View view = list.Views.GetById(viewId); view.ViewFields.RemoveAll(); view.ViewFields.Add("Title"); view.ViewQuery = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">Something Here</Value></Eq></Where>"; view.RowLimit = 10; web.Context.ExecuteQuery();