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(); }
public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation) { using (var scope = new PnPMonitoredScope(this.Name)) { var context = web.Context as ClientContext; web.EnsureProperties(w => w.ServerRelativeUrl, w => w.Url); foreach (PublishingPage page in template.Publishing.PublishingPages) { string parsedFileName = parser.ParseString(page.FileName); string parsedFullFileName = parser.ParseString(page.FullFileName); Microsoft.SharePoint.Client.Publishing.PublishingPage existingPage = web.GetPublishingPage(parsedFileName + ".aspx"); if (!web.IsPropertyAvailable("RootFolder")) { web.Context.Load(web.RootFolder); web.Context.ExecuteQueryRetry(); } if (existingPage != null && existingPage.ServerObjectIsNull.Value == false) { if (!page.Overwrite) { scope.LogDebug( CoreResources.Provisioning_ObjectHandlers_PublishingPages_Skipping_As_Overwrite_false, parsedFileName); continue; } if (page.WelcomePage && web.RootFolder.WelcomePage.Contains(parsedFullFileName)) { //set the welcome page to a Temp page to allow page deletion web.RootFolder.WelcomePage = "home.aspx"; web.RootFolder.Update(); web.Update(); context.ExecuteQueryRetry(); } existingPage.ListItem.DeleteObject(); context.ExecuteQuery(); } web.AddPublishingPage( parsedFileName, page.Layout, parser.ParseString(page.Title) ); Microsoft.SharePoint.Client.Publishing.PublishingPage publishingPage = web.GetPublishingPage(parsedFullFileName); Microsoft.SharePoint.Client.File pageFile = publishingPage.ListItem.File; pageFile.CheckOut(); if (page.Properties != null && page.Properties.Count > 0) { context.Load(pageFile, p => p.Name, p => p.CheckOutType); context.ExecuteQueryRetry(); var parsedProperties = page.Properties.ToDictionary(p => p.Key, p => parser.ParseString(p.Value)); pageFile.SetFileProperties(parsedProperties, false); } if (page.WebParts != null && page.WebParts.Count > 0) { Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager mgr = pageFile.GetLimitedWebPartManager( Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared); context.Load(mgr); context.ExecuteQueryRetry(); AddWebPartsToPublishingPage(page, context, mgr, parser); } List pagesLibrary = publishingPage.ListItem.ParentList; context.Load(pagesLibrary); context.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); } context.ExecuteQueryRetry(); } } return parser; }
public override void ProvisionObjects(Web web, ProvisioningTemplate template) { Log.Info(Constants.LOGGING_SOURCE_FRAMEWORK_PROVISIONING, CoreResources.Provisioning_ObjectHandlers_Pages); var context = web.Context as ClientContext; if (!web.IsPropertyAvailable("ServerRelativeUrl")) { context.Load(web, w => w.ServerRelativeUrl); context.ExecuteQueryRetry(); } foreach (var page in template.PublishingPages) { var url = String.Format("{0}/Pages/{1}.aspx", web.ServerRelativeUrl, page.PageName); if (!url.ToLower().StartsWith(web.ServerRelativeUrl.ToLower())) { url = UrlUtility.Combine(web.ServerRelativeUrl, url); } var exists = true; Microsoft.SharePoint.Client.File file = null; try { file = web.GetFileByServerRelativeUrl(url); web.Context.Load(file); web.Context.ExecuteQuery(); } catch (ServerException ex) { if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException") { exists = false; } } if (exists) { if (page.Overwrite) { file.DeleteObject(); web.Context.ExecuteQueryRetry(); web.AddPublishingPage(page.PageName, page.PageLayoutName, page.Title, page.Content, page.Properties, page.Publish); } //if (file.CheckOutType == CheckOutType.None) //{ // file.CheckOut(); //} } else { web.AddPublishingPage(page.PageName, page.PageLayoutName, page.Title, page.Content, page.Properties, page.Publish); } if (page.WelcomePage) { if (!web.IsPropertyAvailable("RootFolder")) { web.Context.Load(web.RootFolder); web.Context.ExecuteQueryRetry(); } var rootFolderRelativeUrl = url.Substring(web.RootFolder.ServerRelativeUrl.Length); web.SetHomePage(rootFolderRelativeUrl); } // Check out the file if needed if (page.WebParts != null & page.WebParts.Any()) { if (!exists) { file = web.GetFileByServerRelativeUrl(url); web.Context.Load(file); web.Context.ExecuteQuery(); } file.CheckOut(); var existingWebParts = web.GetWebParts(url); foreach (var webpart in page.WebParts) { if (existingWebParts.FirstOrDefault(w => w.WebPart.Title == webpart.Title) == null) { WebPartEntity wpEntity = new WebPartEntity(); wpEntity.WebPartTitle = webpart.Title; wpEntity.WebPartIndex = (int)webpart.Order; wpEntity.WebPartZone = webpart.Zone; if (!string.IsNullOrWhiteSpace(webpart.ListUrl)) { var list = web.GetListByUrl(webpart.ListUrl); var contents = String.Format(webpart.Contents, list.Id, list.Title); wpEntity.WebPartXml = contents.Trim(new[] { '\n', ' ' }); } else { wpEntity.WebPartXml = webpart.Contents.ToParsedString().Trim(new[] { '\n', ' ' }); } //wpEntity.WebPartXml = webpart.Contents.ToParsedString().Trim(new[] {'\n', ' '}); web.AddWebPartToWebPartPage(url, wpEntity); } } file.CheckIn("", CheckinType.MajorCheckIn); file.Publish(""); } } }
private static void EnsureWelcomePage(Web web) { try { Logger.LogInfoMessage(String.Format("Ensuring Welcome Page for {0} ...", web.Url), true); PublishingPage page = web.GetPublishingPage(Constants.WelcomePageName); if (page == null) { Logger.LogInfoMessage("- Creating Welcome Page...", false); web.AddPublishingPage(Constants.WelcomePageName, "PageFromDocLayout", title: "Welcome", publish: false); page = web.GetPublishingPage(Constants.WelcomePageName); if (page == null) { Logger.LogErrorMessage(String.Format("EnsureWelcomePage() failed for {0}: Error=Could not retrieve newly-created Welcome Page.", web.Url), false); return; } Logger.LogInfoMessage("- Editing Welcome Page...", false); // Get parent list of item, this way we can handle all languages var pagesLibrary = page.ListItem.ParentList; web.Context.Load(pagesLibrary); web.Context.ExecuteQueryRetry(); var pageItem = page.ListItem; // Check out the page web.Context.Load(pageItem, p => p.File.CheckOutType); web.Context.ExecuteQueryRetry(); if (pagesLibrary.ForceCheckout || pagesLibrary.EnableVersioning) { if (pageItem.File.CheckOutType == CheckOutType.None) { pageItem.File.CheckOut(); } } // Set the page content string localFilePath = Environment.CurrentDirectory + "\\Pages\\" + Constants.WelcomePageContentFileName; string pageContent = System.IO.File.ReadAllText(localFilePath); pageItem["PublishingPageContent"] = pageContent; pageItem.Update(); Logger.LogInfoMessage("- Publishing Welcome Page...", false); // Check in the page web.Context.Load(pageItem, p => p.File.CheckOutType); web.Context.ExecuteQueryRetry(); if (pageItem.File.CheckOutType != CheckOutType.None) { pageItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn); } // Publish the page pageItem.File.Publish(string.Empty); if (pagesLibrary.EnableModeration) { pageItem.File.Approve(string.Empty); } web.Context.ExecuteQueryRetry(); } Logger.LogInfoMessage("- Establishing Welcome Page...", false); web.SetHomePage("Pages/" + Constants.WelcomePageName); Logger.LogInfoMessage(String.Format("Ensured welcome page for {0} ...", web.Url), false); } catch (Exception ex) { Logger.LogErrorMessage(String.Format("EnsureWelcomePage() failed for {0}: Error={1}", web.Url, ex.Message), false); } }