3

How to create page in pages library using custom page layout in SharePoint 2013? I had created page layout already, I just need to use that page layout and create page using this custom page layout in SharePoint using visual studio?

  • 1
    You can add feature activated event receiver in solution and add page creation code in activated event receiver. You can get info about page creation from this URL: sharepointpals.com/post/… – Dikesh Gandhi Nov 15 '17 at 11:19
2

use below code:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                string currentSite = SPContext.Current.Site.OpenWeb().Title;
                string strPageURL = "MyNewPage.aspx";
                string strPageTitle = "MyCustomPage";
                string strCreatedPageURL = string.Empty;
                string layoutName = GetLayoutName("Article Page");

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                    {
                        Microsoft.SharePoint.Publishing.PublishingSite oPublishingSite = new Microsoft.SharePoint.Publishing.PublishingSite(oSite);

                        SPWeb web = SPContext.Current.Site.OpenWeb();
                        web.AllowUnsafeUpdates = true;
                        SPList PagesList = web.Lists.TryGetList("Pages");

                        if (PagesList != null)
                        {
                            Guid PagesID = PagesList.ID;
                            web.AllProperties["__PagesListId"] = PagesID.ToString();
                            //web.Update();
                            //web.AllProperties["__PublishingFeatureActivated"] = "True";
                            //web.Update();

                            Microsoft.SharePoint.Publishing.PublishingWeb oPublishingWeb = Microsoft.SharePoint.Publishing.PublishingWeb.GetPublishingWeb(web);

                            Microsoft.SharePoint.Publishing.PageLayoutCollection layoutCollection = oPublishingSite.GetPageLayouts(false);

                            Microsoft.SharePoint.Publishing.PageLayout layout = layoutCollection[layoutName];

                            Microsoft.SharePoint.Publishing.PublishingPage page = oPublishingWeb.GetPublishingPage(web.Url + "/Pages/" + strPageURL);

                            if (page == null)
                            {
                                page = oPublishingWeb.AddPublishingPage(strPageURL, layout);
                            }
                            else
                            {
                                throw new Exception("A page with the given URL already exists. Please try a different URL");
                            }

                            if (page.ListItem.File.CheckedOutByUser == null)
                            {
                                page.CheckOut();
                            }


                            page.Title = strPageTitle;
                            page.Update();
                            page.CheckIn("Page Created");


                            //page.ListItem.File.Publish("Page Published");

                            web.AllowUnsafeUpdates = false;
                            strCreatedPageURL = web.Url + "/Pages/" + strPageURL;
                        }



                    }
                });


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        private string GetLayoutName(string contentTypeName)
        {
            string layoutURL = string.Empty;
            switch (contentTypeName)
            {
                case "Article":
                    layoutURL = "/_catalogs/masterpage/About CAA.aspx";
                    break;
                case "Convention":
                    layoutURL = "/_catalogs/masterpage/About CAA.aspx";
                    break;

                default:
                    layoutURL = "/_catalogs/masterpage/About CAA.aspx";
                    break;
            }

            return layoutURL;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.