我試圖從一個子網站的自定義佈局頁面獲取發佈網站(Microsoft.SharePoint.Publishing.PublishingWeb
)中的所有頁面佈局時最近遇到此錯誤。該代碼使用從Enterprise Wiki站點模板創建的站點在VM服務器上工作。但是,在開發服務器上運行時,代碼會遇到各種異常。NullReferenceException引發「GetAvailablePageLayouts」
爲了解決這個錯誤,我編寫了三種不同的方法。所有這三個人都在VM中工作,但是在開發服務器中使用這三者時都會引發異常。例如:
private PageLayout FindPageLayout(PublishingWeb pubWeb, string examplePage)
{
/* The error occurs in this method */
if (pubWeb == null)
throw new ArgumentException("The pubWeb argument cannot be null.");
PublishingSite pubSiteCollection = new PublishingSite(pubWeb.Web.Site);
string pageLayoutName = "EnterpriseWiki.aspx"; // for testing purposes
PageLayout layout = null;
/* Option 1: Get page layout from collection with name of layout used as index
* Result: System.NullReferenceException from GetPageLayouts()
*/
layout = pubSiteCollection.PageLayouts["/_catalogs/masterpage/"+pageLayoutName];
/* Option 2: Bring up an existing publishing page, then find the layout of that page using the Layout property
* Result: Incorrect function COM exception
*/
SPListItem listItem = pubWeb.Web.GetListItem(examplePage);
PublishingPage item = PublishingPage.GetPublishingPage(listItem);
layout = item.Layout;
/* Option 3: Call "GetPageLayouts" and iterate through the results looking for a layout of a particular name
* Result: System.NullReferenceException thrown from Microsoft.SharePoint.Publishing.PublishingSite.GetPageLayouts()
*/
PageLayoutCollection layouts = pubSiteCollection.GetPageLayouts(true);
for(int i = 0; i < layouts.Count; i++)
{
// String Comparison based on the Page Layout Name
if (layouts[i].Name.Equals(pageLayoutName, StringComparison.InvariantCultureIgnoreCase))
layout = layouts[i];
}
return layout;
}