サブサイトのカスタムレイアウトページからパブリッシュサイト(Microsoft.SharePoint.Publishing.PublishingWeb
)のすべてのページレイアウトを取得しようとすると、最近このエラーが発生しました。このコードは、エンタープライズWikiサイトテンプレートから作成されたサイトを使用してVMサーバー上で動作しました。しかし、開発サーバー上で実行しているときには、コードでさまざまな例外が発生しました。"GetAvailablePageLayouts"でNullReferenceExceptionがスローされました
エラーを回避しようとすると、クラスを3つの異なる方法でコーディングしました。 3人すべてがVMで働いていましたが、3人すべてが開発サーバーで使用されたときに例外を投げました。例:
はあなたがしていることPublishingWeb.GetPublishingWeb(SPWeb)メソッドを呼び出すことで、あなたの「PublishingWeb」オブジェクトを取得しているときことを確認してください:ここで
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;
}