Partager via


Create and Publish web pages in Publishing SharePoint sites programmatically

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("https://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");
    }
}

This is an easy way of automating creating & publishing pages in collaboration/publishing sites, especially when the need is to create/publish them at a larger scale.  I hope this tip is helpful!

Comments

  • Anonymous
    June 21, 2008
    PingBack from http://wordnew.acne-reveiw.info/?p=1312

  • Anonymous
    July 31, 2008
    Hi Sridhar, Can we design a web user control and add this to a web part in SharePoint? Please pass me some info.

  • Anonymous
    August 01, 2008
    Sridhar, Is there a way to add data View webparts and Custom webparts to the newly added page programmatically? I have a dataview webpart that consumes a webservice.

  • Anonymous
    August 02, 2008
    Hi Ganapati, Yes, you can embed an user control within SharePoint web parts.  Here are a few references you can make use of: http://msdn.microsoft.com/en-us/magazine/cc300767.aspx http://msdn.microsoft.com/en-us/library/w9b5ett0.aspx http://www.helloitsliam.com/archive/2008/01/21/moss2007-–-user-controls-within-web-part-part-2.aspx http://jopx.blogspot.com/2006/09/usercontrols-ascx-webparts-and.html You can also use SmartPart: http://www.codeplex.com/smartpart/Release/ProjectReleases.aspx?ReleaseId=10697 Cheers, Sridhar

  • Anonymous
    August 02, 2008
    Hi Karthik, Yes you can make use of SPWebPartManager class and add web parts to different zones at runtime. Sorry, I don't have any code samples yet to show how to do it, but it's possible. Cheers, Sridhar

  • Anonymous
    August 25, 2009
    Hi, The way you have created a new publishing page, is there a way I can add it to a document library somehow? or is there any other way of doing it? I want to add new .aspx page in a document library that has content type set on it. Any help would be great. Thanks, Priyanka

  • Anonymous
    March 30, 2010
    Hi,   i've a table in sql database,i want to access that data and display in a list in sharepoint.what is the code? (without using BDC) any help would be great  Thanks   Ranju

  • Anonymous
    November 11, 2010
    Can we use share point to develop a social network and publish it on the internet so every one can use it with their user name and password

  • Anonymous
    November 11, 2010
    Can we use share point to develop a social network and publish it on the internet so every one can use it with their user name and password