Welcome to MSDN Blogs Sign in | Join | Help

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("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");
    }
}
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!
Published Saturday, June 21, 2008 5:22 PM by sridhara

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Pregnant Man » Create and Publish web pages in Publishing SharePoint sites programmatically

Thursday, July 31, 2008 11:35 AM by Ganapati

# re: Create and Publish web pages in Publishing SharePoint sites programmatically

Hi Sridhar,

Can we design a web user control and add this to a web part in SharePoint?

Please pass me some info.

Friday, August 01, 2008 4:08 PM by karthik

# re: Create and Publish web pages in Publishing SharePoint sites programmatically

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.

Saturday, August 02, 2008 9:15 AM by sridhara

# re: Create and Publish web pages in Publishing SharePoint sites programmatically

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

Saturday, August 02, 2008 9:18 AM by sridhara

# re: Create and Publish web pages in Publishing SharePoint sites programmatically

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

Wednesday, August 26, 2009 7:14 AM by Priyanka

# re: Create and Publish web pages in Publishing SharePoint sites programmatically

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

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker