我使用以下代碼根據被認爲已經設置並基於內容類型的頁面佈局創建發佈頁面。代碼爲您的特徵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("");
}