Friday, August 28, 2009

Hello All,

I wanted to create publishing pages that is aspx pages in the Pages library. After lots of tricks here and there, finally managed to do it through the following piece of code:
using Microsoft.SharePoint.Publishing;
PublishingSite pSite = new PublishingSite(siteurl);
SPContentType projectCT = pSite.ContentTypes[];
PageLayoutCollection pageLayouts = pSite.GetPageLayouts(projectCT, true);
PageLayout projectLayout = pageLayouts[];
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(hlfWeb);
PublishingPageCollection pageCollection = pWeb.GetPublishingPages();
PublishingPage projectPage = pageCollection.Add("page1.aspx", projectLayout);
SPListItem projectPageItem = projectPage.ListItem;
projectPageItem["Title"] = "page1";
//other properties can be given here as well.

Requirement was such that I had to create these pages in specific folders. A simple way out:
PublishingPage projectPage = pageCollection.Add(projectFolder.ServerRelativeUrl + "/" + projectDetail.ProjectTitle + ".aspx", projectLayout);

Where projectFolder is the folder in the library.

Hope it saves on time for somebody with similar problem.
Happy Sharepointing!!

No comments:

Post a Comment