Programmatically Adding a Publishing Page
-
16-10-2019 - |
Question
Has anyone at all look at uploading a file to/using an existing SPFile in the PublishingPageCollection.Add() method?
I'm referring to this override specifically: http://msdn.microsoft.com/en-us/library/ms495757.aspx
The docs just say "File to convert to get the Page content" - how does it convert it? Does it take an existing Publishing Page just full of the mso: meta tags filled with content and bung it into the specified layout or?
But James, Why!?
Got a requirement where I'd like to be able to very quickly knock up a site structure, including sticking in some pre-created Publishing Pages into the site as test data (saves me creating lots of "testarticles").
Solution
This would be using the Document Conversion Service, wouldn't it? I can't imagine why you'd want to convert an existing Publishing Page to ... a Publishing Page. It think that overload might be for Document conversion.
I would suggest uploading to a document library and then invoking the Document Conversion service.
I have added a page programmatically - but not converting anything:
string fileName = "Somefile.aspx";
string contentTypeString = "0x010100C568DB52D9D0A14D9B2FDCC96666E9F2";
string pageLayoutName = "HomeLayout.aspx";
string newPageTitle = "Some Title";
PublishingWeb elevPubWeb = PublishingWeb.GetPublishingWeb(elevWeb);
SPContentTypeId cTypeId = new SPContentTypeId(contentTypeString);
PageLayout[] layouts = elevPubWeb.GetAvailablePageLayouts(cTypeId);
PageLayout layout = layouts[1];
foreach (PageLayout p in layouts)
{
if (p.Name == pageLayoutName)
{
layout = p;
break;
}
}
try
{
PublishingPageCollection ppc = elevPubWeb.GetPublishingPages();
PublishingPage newPage = ppc.Add(fileName, layout);
newPage.Title = newPageTitle;
newPage.Update();
}
catch (Exception ex)
{
//Handle page layout not found
}