我使用以下代码基于页面布局创建发布页面,该页面布局被认为已配置并基于内容类型。代码在您的功能的 FeatureActivated 事件处理程序中运行:
using (SPWeb ParentWeb = properties.Feature.Parent as SPWeb)
{
PublishingWeb webpublish = PublishingWeb.GetPublishingWeb(ParentWeb);
//retrieve the layout associated with our custom content type
PageLayout[] layouts = webpublish.GetAvailablePageLayouts(new SPContentTypeId(MyContentTypeID));
//first layout considered, as this is the one created by this feature
PageLayout MyPageLayout = layouts[0];
PublishingPageCollection PublishingPages = webpublish.GetPublishingPages();
PublishingPage newPage = PublishingPages.Add("NewPublishingPageName.aspx", MyPageLayout);
newPage.Title = "My first publishing page";
newPage.ListItem.Update();
//check-in and republish the page
SPFile listItemFile = newPage.ListItem.File;
//check that the file is not checked out - if it is, check it in.
if (listItemFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
listItemFile.CheckIn("Initial default content added.");
}
listItemFile.Publish("");
listItemFile.Approve("");
}