Thursday, October 29, 2009

Creating a SharePoint page using Object Model

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb web = SPContext.Current.Web.Site.RootWeb;
//SPContext.Current.Web;
using (web)
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Pages"]
String url = list.RootFolder.ServerRelativeUrl.ToString();
// 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 = null;
foreach (PageLayout _layout in layouts)
{
//set the page layout to your site's default layout
if
(_layout.Name == "yourpagelayout.aspx")
{
layout = _layout;
break;
}
}
PublishingPage newPage = publishingWeb.GetPublishingPages().Add("sample.aspx", layout);
// setting the URL value to the newPageUrl for redirecting the user after creating it
//newPageUrl = web.Url + "/" + newPage.Url;
newPage.Description = "Sample page from code behind Dynamic Page";
newPage.Title = "Dynamic 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");
}
}
});

1 comment:

digital signature PDF said...

Very helpful article ! I was always curious about all these complex algorithms that are being used in these ssl encryptions.