Welcome to MSDN Blogs Sign in | Join | Help

create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Well, we all know how we can create publishing pages in portal sites from UI. In portal sites, whenever we create a publishing page we need provide the name, title, description and a layout page. Once if we created the publishing page then we can check in, approve and publish it. All those steps we can do programmatically. Below I am giving the code snippet for creating publishing pages programmatically (add it into a specific folder) and after that redirect the user to that page with edit mode.

<codeSnippet>  

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing;

namespace ConsoleApplication3

{

class Program

{

static void Main(string[] args)

{

string newPageUrl = "";

SPSecurity.RunWithElevatedPrivileges(delegate()// executing this code with elevated privileges will help whenever we create the publishing pages from a sharepoint portal site for an another portal site.If we create the publishing page within a site collection then this code will execute fine without elevated privileges.

{

using (SPSite site = new SPSite("http://sigr8-1b:45907/sites/sas")) // provide your portal site URL

{

using (SPWeb web = site.OpenWeb())

{

web.AllowUnsafeUpdates = true;

SPList list = web.Lists["Pages"];

String url = list.RootFolder.ServerRelativeUrl.ToString();

// create a new folder

string strFolderName = "Folder7" ;

SPListItem newFolder = list.Items.Add(url, SPFileSystemObjectType.Folder, strFolderName);

newFolder.Update();

// if you want to add the new page in a existing folder you can take it by it's ID like below

//SPListItem newFolder = list.Folders.GetItemById(3);

// creating a publishing web

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);

PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts();

PageLayout layout = layouts[0];

string pageName = "MyPublishingPage5.aspx";

 

PublishingPage newPage = publishingWeb.GetPublishingPages().Add(newFolder.Folder.ServerRelativeUrl + "/"+ pageName, layout);

// if you want to add the new page direclty inside the pages library then you can use the below line of code

//PublishingPage newPage = publishingWeb.GetPublishingPages().Add(pageName, layout);

 

// setting the URL value to the newPageUrl for redirecting the user after creating it

newPageUrl = web.Url + "/" + newPage.Url;

newPage.Description = "This my sample publishing page";

newPage.Title = "My Publishing Page";

newPage.Update();

 

//Now we can checkin the newly created page to the “pages” library

SPFile pageFile = newPage.ListItem.File;

if (pageFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)

{

pageFile.CheckIn("CheckedIn");

pageFile.Publish("publihsed");

}

// If you are executing this code in a web app then if you want to redirect the user to the newly created page with edit mode. Remember, we need to checkout the page before redireting the user to that page in edit mode, other wise you will get an Authoring error with message “You have not checked out this page. Click ‘Edit Page’ to edit the page.”

pageFile.CheckOut();

Response.Redirect(newPageUrl + "?ControlMode=Edit&DisplayMode=Design");

}

}

});

}

}

}

</codeSnippet> 

Happy coding !

Published Saturday, March 15, 2008 9:29 PM by sowmyancs

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

# MSDN Blog Postings &raquo; create publishing pages in portal sites programmatically

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

I think subfolders inside Pages Library are not supported .. correct me If I am wrong ...

Wednesday, January 07, 2009 1:31 AM by Madhur

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Subfolders are not supported by Microsoft:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;948614

Be very cautious while using subfolders.

Saturday, February 07, 2009 2:43 PM by MB

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Thanks sowmyancs

I used this to create pages in other Library than Pages.

Wednesday, March 04, 2009 4:03 PM by Preet

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Publishing the pages (as listed in the code above) leaves the pages in a "pending" approval status.  Is there any way to programatically approve the pages as well?  I've looked but so far can find no way to do this.  I want to generate lots of publishing pages programatically and so it'd be great if I didn't have to click on each one and approve them through the UI.  I've looked into the WorkflowManager object but it only seems to have methods to start or cancel a workflow.  Thanks much!

Thursday, May 07, 2009 8:33 AM by Phil

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Found it - just use the Publish and Approve methods of the SPFile class:

publishingPage.CheckIn(checkInComment);

SPFile pageFile = publishingPage.ListItem.File;

           pageFile.Publish(checkInComment);

           pageFile.Approve(checkInComment);

Listed here: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingpage.description.aspx

Thursday, May 07, 2009 4:28 PM by Phil

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Is there any way to do similar code

on client application (I'd like

implement some functionality into

TFS UI Control under Visual Studio

Addin)

Cheers,

Peter

Thursday, June 25, 2009 7:17 AM by Peter Volsik

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Is there any way to do similar code

on client application (I'd like

implement some functionality into

TFS UI Control under Visual Studio

Addin)?

Cheers,

Peter

Thursday, June 25, 2009 7:17 AM by Peter Volsik

# re: create publishing pages in portal sites programmatically and add it into a specific folders inside the Pages library

Hi i am following this approach . But pages created by this way have error . after creating page if i browse it shows me error page not found , but it is there in subfolder

Friday, November 13, 2009 4:24 AM by anoymous

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker