private static void RemovePublishingPage(SPPublishing.PublishingPage publishingPage, PublishingPage page, ClientContext ctx, Web web) { if (publishingPage != null && publishingPage.ServerObjectIsNull.Value == false) { if (!web.IsPropertyAvailable("RootFolder")) { web.Context.Load(web.RootFolder); web.Context.ExecuteQueryRetry(); } if (page.Overwrite) { if (page.WelcomePage && web.RootFolder.WelcomePage.Contains(page.FileName + ".aspx")) { //set the welcome page to a Temp page to allow remove the page web.RootFolder.WelcomePage = "home.aspx"; web.RootFolder.Update(); web.Update(); ctx.Load(publishingPage); ctx.ExecuteQuery(); } publishingPage.ListItem.DeleteObject(); ctx.ExecuteQuery(); } else { return; } } }
private static void AddPublishingPage(ClientContext context, PublishingWeb webPub, ListItemCollection allPageLayouts, OfficeDevPnP.Core.Framework.Provisioning.Model.PublishingPage page) { ListItem layout = null; //try to get layout by Title layout = allPageLayouts.Where(x => x["Title"] != null && x["Title"].ToString().Equals(page.Layout)).FirstOrDefault(); //try to get layout by DisplayName if (layout == null) { layout = allPageLayouts.Where(x => x.DisplayName == page.Layout).FirstOrDefault(); } //we need to have a layout for a publishing page if (layout == null) { throw new ArgumentNullException(string.Format("Layout '{0}' for page {1} can not be found.", page.Layout, page.Name)); } context.Load(layout); // Create a publishing page PublishingPageInformation publishingPageInfo = new PublishingPageInformation(); publishingPageInfo.Name = page.Name; publishingPageInfo.PageLayoutListItem = layout; Microsoft.SharePoint.Client.Publishing.PublishingPage publishingPage = webPub.AddPublishingPage(publishingPageInfo); //publishingPage.ListItem.File.Approve(string.Empty); context.Load(publishingPage); context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl); context.ExecuteQuery(); }
public static void AddPublishingPage(PublishingPage page, ClientContext ctx, Web web) { SPPublishing.PublishingPage publishingPage = web.GetPublishingPage(page.FileName + ".aspx"); RemovePublishingPage(publishingPage, page, ctx, web); web.AddPublishingPage(page.FileName, page.Layout, page.Title, false); //DO NOT Publish here or it will fail if library doesn't enable Minor versions (PnP bug) publishingPage = web.GetPublishingPage(page.FileName + ".aspx"); Microsoft.SharePoint.Client.File pageFile = publishingPage.ListItem.File; pageFile.CheckOut(); if (page.Properties != null && page.Properties.Count > 0) { ctx.Load(pageFile, p => p.Name, p => p.CheckOutType); //need these values in SetFileProperties ctx.ExecuteQuery(); pageFile.SetFileProperties(page.Properties, false); } if (page.WebParts != null && page.WebParts.Count > 0) { Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager mgr = pageFile.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared); ctx.Load(mgr); ctx.ExecuteQuery(); AddWebpartsToPublishingPage(page, ctx, mgr); } List pagesLibrary = publishingPage.ListItem.ParentList; ctx.Load(pagesLibrary); ctx.ExecuteQueryRetry(); ListItem pageItem = publishingPage.ListItem; web.Context.Load(pageItem, p => p.File.CheckOutType); web.Context.ExecuteQueryRetry(); if (pageItem.File.CheckOutType != CheckOutType.None) { pageItem.File.CheckIn(String.Empty, CheckinType.MajorCheckIn); } if (page.Publish && pagesLibrary.EnableMinorVersions) { pageItem.File.Publish(String.Empty); if (pagesLibrary.EnableModeration) { pageItem.File.Approve(String.Empty); } } if (page.WelcomePage) { SetWelcomePage(web, pageFile); } ctx.ExecuteQuery(); }