Hi Friends,
This blog will help you when you need to create pages in based on page layout.You just need to use the below common method.
This blog will help you when you need to create pages in based on page layout.You just need to use the below common method.
private void CreatePublishingPage(string absoulteUrl, string pageName, string pageLayoutName, bool isLandingPage)Hope this blog will help you.You can use this code as reusable and use wherever you want.Give me shout if you need clarification.
{
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();
//GetPageLayoutName(application);
if (!string.IsNullOrEmpty(pageLayoutName))
{
/* Search for the page layout for creating the new page */
Listlayouts = 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.Update();
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;
}
publishingWeb.Update();
}
}
}
}
}
Comments
Post a Comment