0

I am trying to create new pages in my publishing site(need to use a custom page layout) using SharePoint Server Object Model. I couldn't find much help on this topic from internet.

Happy to have some references from you.

Biju Joseph - MCSD
  • 1,081
  • 8
  • 16

1 Answers1

4

Microsoft.SharePoint.Publishing is the assembly that we need to use to get pages created and published. A sample is provided below:

    using (SPSite site = new SPSite("http://moss"))
    {
    using (SPWeb web = site.OpenWeb())
    {
    PublishingSite pSite = new PublishingSite(site);
    SPContentType ctype = pSite.ContentTypes["Welcome Page"];
    PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
    PageLayout pageLayout = pageLayouts["/_catalogs/masterpage/welcomesplash.aspx"];
    PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
    PublishingPageCollection pPages = pWeb.GetPublishingPages();
    PublishingPage pPage = pPages.Add("Programmatic_Test.aspx", pageLayout);
    SPListItem newpage = pPage.ListItem;
    newpage["Title"] = "Page added programmatically";
    newpage.Update();

    newpage.File.CheckIn("all looks good");
    newpage.File.Publish("all looks good");
    }
    }

reference

Amit
  • 2,019
  • 1
  • 17
  • 23