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(); } }
private List<PublishingPage> GetPublishingPagesListFromConfiguration() { List<PublishingPage> pages = new List<PublishingPage>(); XNamespace ns = "http://schemas.somecompany.com/PublishingPageProvisioningExtensibilityHandlerConfiguration"; XDocument doc = XDocument.Parse(configurationXml); foreach (var p in doc.Root.Descendants(ns + "Page")) { PublishingPage page = new PublishingPage { Title = p.Attribute("Title").Value, Layout = p.Attribute("Layout").Value, Overwrite = bool.Parse(p.Attribute("Overwrite").Value), FileName = p.Attribute("FileName").Value, Publish = bool.Parse(p.Attribute("Publish").Value) }; if (p.Attribute("WelcomePage") != null) { page.WelcomePage = bool.Parse(p.Attribute("WelcomePage").Value); } var pageContentNode = p.Descendants(ns + "PublishingPageContent").FirstOrDefault(); if (pageContentNode != null) { page.PublishingPageContent = pageContentNode.Attribute("Value").Value; } foreach (var wp in p.Descendants(ns + "WebPart")) { PublishingPageWebPart publishingPageWebPart = new PublishingPageWebPart(); if (wp.Attribute("DefaultViewDisplayName") != null) { publishingPageWebPart.DefaultViewDisplayName = wp.Attribute("DefaultViewDisplayName").Value; } publishingPageWebPart.Order = uint.Parse(wp.Attribute("Order").Value); publishingPageWebPart.Title = wp.Attribute("Title").Value; publishingPageWebPart.Zone = wp.Attribute("Zone").Value; string webpartContensts = wp.Element(ns + "Contents").Value; publishingPageWebPart.Contents = webpartContensts.Trim(new[] { '\n', ' ' }); page.WebParts.Add(publishingPageWebPart); } Dictionary<string, string> properties = new Dictionary<string, string>(); foreach (var property in p.Descendants(ns + "Property")) { properties.Add( property.Attribute("Name").Value, property.Attribute("Value").Value); } page.Properties = properties; pages.Add(page); } return pages; }