Create Publishing Pages on SharePoint Online Site

Sometimes you need to create sample pages. It will take a lot of time to do this manually. If you want to create sample pages on SharePoint Online, the code block below will help you with this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;

namespace CreatePageFromLayout
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new ClientContext("https://domain.sharepoint.com/sites/samplesite"))
            {
                SecureString password = GetPasswordFromConsoleInput();
                ctx.Credentials = new SharePointOnlineCredentials("sitecollectionadmin@domain.com", password);

                try
                {
                    var web = ctx.Web;
                    ctx.Load(web);

                    PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(ctx, web);
                    ctx.Load(publishingWeb);

                    if (publishingWeb != null)
                    {
                        string pageLayoutDisplayName = "PageLayout_Name";
                        List publishingLayouts = ctx.Site.RootWeb.Lists.GetByTitle("Masterpage Gallery");
                        ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());

                        ctx.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageLayoutDisplayName));
                        ctx.ExecuteQuery();

                        ListItem layout = allItems.Where(x => x.DisplayName == pageLayoutDisplayName).FirstOrDefault();
                        ctx.Load(layout);

                        List pages = ctx.Site.RootWeb.Lists.GetByTitle("Pages");
                        List<TemplateClass> templateList = new List<TemplateClass>();

                        templateList.Add(new TemplateClass() { PageName = "SamplePage1" });
                        templateList.Add(new TemplateClass() { PageName = "SamplePage2" });

                        foreach (TemplateClass t in templateList)
                        {
                            PublishingPageInformation publishingPageInfo = new PublishingPageInformation();                            
                            publishingPageInfo.Name = String.Format("{0}.aspx", t.PageName.Replace(" ", ""));
                            publishingPageInfo.PageLayoutListItem = layout;

                            PublishingPage publishingPage = publishingWeb.AddPublishingPage(publishingPageInfo);

                            List publishPageParentList = publishingPage.ListItem.ParentList;
                            ctx.Load(publishPageParentList, list => list.EnableModeration);
                            ctx.ExecuteQuery();

                            publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
                            publishingPage.ListItem.File.Publish(string.Empty);

                            if (publishingPage.ListItem.ParentList.EnableModeration)
                                publishingPage.ListItem.File.Approve(string.Empty);

                            ctx.Load(publishingPage);
                            ctx.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
                            ctx.ExecuteQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }

                Console.ReadLine();
             }
        }

        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }

    public class TemplateClass
    {
        public string PageName { get; set; }
    }
}

Add a Comment

Your email address will not be published. Required fields are marked *