.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Create Publishing page based on page layout in sharepoint2010

Posted By:Ravishankar       Posted Date: April 16, 2011    Points: 40    Category: SharePoint    URL: http://www.dotnetspark.com  

Reusable code for creating Publishing page based on selected page layout.

private void CreatePublishingPage(string absoulteUrl, string pageName, string pageLayoutName, bool isLandingPage)
using (SPSite Osite = new SPSite(absoulteUrl))
using (SPWeb oWeb = Osite.OpenWeb())
string fullPageUrl = string.Empty;
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(oWeb);
/* Get the publishing web page collection list. */
PublishingPageCollection publishingPageCollection = publishingWeb.GetPublishingPages();
if (!string.IsNullOrEmpty(pageLayoutName))
/* Search for the page layout for creating the new page */
List layouts = new List(publishingWeb.GetAvailablePageLayouts());
PageLayout pageLayout = layouts.Find(
delegate(PageLayout l)
return l.Name.Equals(pageLayoutName, StringComparison.CurrentCultureIgnoreCase);
/*page layout exists*/
if (pageLayout != null)
PublishingPage newPage = null;
newPage = publishingPageCollection.Add(pageName + ".aspx", pageLayout);
newPage.Title = pageName;
newPage.CheckIn("page checked in");
newPage.ListItem.File.Publish("page published");
newPage.ListItem.File.Approve("page approved");
/* Set newly created page as a welcome page */
if (isLandingPage == true)
fullPageUrl = oWeb.Url + "/Pages/" + pageName + ".aspx";
SPFile fileNew = publishingWeb.Web.GetFile(fullPageUrl);
publishingWeb.DefaultPage = fileNew;



Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend