Reputation: 1541
Not sure if this is asked, but searching hasn't quite yielded what I'm looking for. I have a page layout already, what I need to do is programmatically create a page in the Pages library.
I'm fuzzy on the details, but somehow I think I will need to open the Layout, then stream it to a page and then save the page. I'm unsure how to go about this.
The page is context sensitive so I think I'll begin with using SPSite and SPWeb to get access to the lists.
What I'm unclear on is, how can I get the Layouts? I think I should be able to add a page somewhat like this:
SPWeb web = SPContext.Current.Site.OpenWeb();
SPList Pages = web.Lists["Pages"];
SPListItemCollection splc = Pages.Items;
foreach (SPListItem spli in splc)
{
if (spli.Name == "lmIntraTopicsArticle")
{
}
}
SPListItem sli = splc.Add();
Pages.Update();
SPFolder PagesFolder = Pages.RootFolder;
byte[] layoutContents = new byte[20];
SPFile myNewPage = PagesFolder.Files.Add(PagesFolder.Url + "/TopicWindowArchive.aspx", layoutContents);
web.Update();
Now I need to figure out how to add content from a layout. Update in a few if I figure it out.
Thank you,
Upvotes: 2
Views: 4744
Reputation: 14295
The trick is to get a PublishingWeb object. That contains the layouts.
See here for an example
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
string pageName = “MyCustomPage.aspx”;
PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();
PageLayout currPageLayout = pageLayouts[0];
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
PublishingPage newPage = pages.Add(pageName,currPageLayout);
newPage.ListItem[FieldId.PublishingPageContent] = “This is my content”;
newPage.ListItem.Update();
newPage.Update();
newPage.CheckIn(“This is just a comment”);
Also check this answer
Upvotes: 5