In this example I'm using again the powerfull extensions methods of OfficeAMS.
In SharePoint 2013 list view web parts are still the same xsltlistviewwebparts that we are used to work with. The only difference in this new version is that we have the ability to overwrite the rendering on the client side by making use of JS Link.
A little bit of background information: a remote event receiver is binded to a list, OnItemAdded, I'm creating a new SharePoint site which I'm provisioning with CSOM. I need a document library, a page and a ListViewWebPart of the document library on the wiki page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties) | |
{ | |
var result = new SPRemoteEventResult(); | |
// Get the token from the request header. Because this is a .svc remote event receiver we use the current Operationcontext. | |
var requestProperty = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name]; | |
var contextToken = TokenHelper.ReadAndValidateContextToken(requestProperty.Headers["X-SP-ContextToken"], requestProperty.Headers[HttpRequestHeader.Host]); | |
using (var clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) | |
{ | |
if (clientContext != null) | |
{ | |
try | |
{ | |
//create document library | |
clientContext.Web.AddDocumentLibrary("Internal documents", true); | |
//create wiki page | |
clientContext.Web.AddWikiPage("Site Pages", "Documents.aspx"); | |
//set correct page layout to wiki page | |
clientContext.Web.AddLayoutToWikiPage("sitepages", WikiPageLayout.TwoColumns, "Documents"); | |
//get new document library | |
var internallDocLib = clientContext.Web.GetListByTitle("Internal documents"); | |
//add listviewwebparts to page | |
var internalDocsWebPartEntity = new WebPartEntity | |
{ | |
WebPartIndex = 2, | |
WebPartTitle = "Latest Internal Documents", | |
WebPartZone = "Left", | |
WebPartXml = string.Format(Globals.ListViewWebPart, internallDocLib.Id, "Latest Internal Documents") | |
}; | |
//params: pageslibrary url, webpartentity, wiki page, table row, table column, addSpace under web part | |
clientContext.Web.AddWebPartToWikiPage("sitepages", internalDocsWebPartEntity, "documents.aspx", 1, 1, true); | |
} | |
catch | |
{ | |
//write logging information | |
} | |
} | |
} | |
} |
You'll have noticed that the WebPartXml property of the WebPartEntity contains a reference to my globals class. In this class, I've created an xml snippet which I can reuse if I want to add a xsltlistviewwebpart to a page. Using this snippet requires 2 params, the List.Id and a title for the web part. Feel free to adapt this snippet for your requirements (toolbar, JS Link reference...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Globals | |
{ | |
//WebParts | |
public const string ListViewWebPart = "<webParts>" + | |
"<webPart xmlns='http://schemas.microsoft.com/WebPart/v3'>" + | |
"<metaData>" + | |
"<type name='Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' />" + | |
"<importErrorMessage>Cannot import this Web Part.</importErrorMessage>" + | |
"</metaData>" + | |
"<data>" + | |
"<properties>" + | |
"<property name='ShowWithSampleData' type='bool'>False</property>" + | |
"<property name='Default' type='string' />" + | |
"<property name='NoDefaultStyle' type='string' null='true' />" + | |
"<property name='CacheXslStorage' type='bool'>True</property>" + | |
"<property name='ViewContentTypeId' type='string' />" + | |
"<property name='XmlDefinitionLink' type='string' />" + | |
"<property name='ManualRefresh' type='bool'>False</property>" + | |
"<property name='ListUrl' type='string' />" + | |
"<property name='ListId' type='System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'>{0}</property>" + | |
"<property name='TitleUrl' type='string'></property>" + | |
"<property name='EnableOriginalValue' type='bool'>False</property>" + | |
"<property name='Direction' type='direction'>NotSet</property>" + | |
"<property name='ServerRender' type='bool'>False</property>" + | |
"<property name='ViewFlags' type='Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'>Html, TabularView, Hidden, Mobile</property>" + | |
"<property name='AllowConnect' type='bool'>True</property>" + | |
"<property name='ListName' type='string'>{0}</property>" + | |
"<property name='ListDisplayName' type='string' />" + | |
"<property name='Title' type='string'>{1}</property>" + | |
"</properties>" + | |
"</data>" + | |
"</webPart>" + | |
"</webParts>"; | |
} |