private static void AddWebpartsToPublishingPage(PublishingPage page, ClientContext ctx, Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager mgr) { foreach (var wp in page.WebParts) { string wpContentsTokenResolved = wp.Contents; Microsoft.SharePoint.Client.WebParts.WebPart webPart = mgr.ImportWebPart(wpContentsTokenResolved).WebPart; Microsoft.SharePoint.Client.WebParts.WebPartDefinition definition = mgr.AddWebPart( webPart, wp.Zone, (int)wp.Order ); var webPartProperties = definition.WebPart.Properties; ctx.Load(definition.WebPart); ctx.Load(webPartProperties); ctx.ExecuteQuery(); if (wp.IsListViewWebPart) { AddListViewWebpart(ctx, wp, definition, webPartProperties); } } }
private static void AddListViewWebpart( ClientContext ctx, PublishingPageWebPart wp, Microsoft.SharePoint.Client.WebParts.WebPartDefinition definition, PropertyValues webPartProperties) { string defaultViewDisplayName = wp.DefaultViewDisplayName; if (!String.IsNullOrEmpty(defaultViewDisplayName)) { string listUrl = webPartProperties.FieldValues["ListUrl"].ToString(); ctx.Load(definition, d => d.Id); // Id of the hidden view which gets automatically created ctx.ExecuteQuery(); Guid viewId = definition.Id; List list = ctx.Web.GetListByUrl(listUrl); Microsoft.SharePoint.Client.View viewCreatedFromWebpart = list.Views.GetById(viewId); ctx.Load(viewCreatedFromWebpart); Microsoft.SharePoint.Client.View viewCreatedFromList = list.Views.GetByTitle(defaultViewDisplayName); ctx.Load( viewCreatedFromList, v => v.ViewFields, v => v.ListViewXml, v => v.ViewQuery, v => v.ViewData, v => v.ViewJoins, v => v.ViewProjectedFields); ctx.ExecuteQuery(); //need to copy the same View definition to the new View added by the Webpart manager viewCreatedFromWebpart.ViewQuery = viewCreatedFromList.ViewQuery; viewCreatedFromWebpart.ViewData = viewCreatedFromList.ViewData; viewCreatedFromWebpart.ViewJoins = viewCreatedFromList.ViewJoins; viewCreatedFromWebpart.ViewProjectedFields = viewCreatedFromList.ViewProjectedFields; viewCreatedFromWebpart.ViewFields.RemoveAll(); foreach (var field in viewCreatedFromList.ViewFields) { viewCreatedFromWebpart.ViewFields.Add(field); } //need to set the JSLink to the new View added by the Webpart manager. //This is because there's no way to change the BaseViewID property of the new View, //and we needed to do that because the custom JSLink was bound to a specific BaseViewID (overrideCtx.BaseViewID = 3;) //The work around to this is to add the JSLink to the specific new View created when you add the xsltViewWebpart to the page //and remove the "overrideCtx.BaseViewID = 3;" from the JSLink file //that way, the JSLink will be executed only for this View, that is only used in the xsltViewWebpart, //so the effect is the same that bind the JSLink to the BaseViewID if (webPartProperties.FieldValues.ContainsKey("JSLink") && webPartProperties.FieldValues["JSLink"] != null) { viewCreatedFromWebpart.JSLink = webPartProperties.FieldValues["JSLink"].ToString(); } viewCreatedFromWebpart.Update(); ctx.ExecuteQuery(); } }