我最近在尝试Microsoft.SharePoint.Publishing.PublishingWeb
从子站点中的自定义布局页面获取发布站点 ( ) 中的所有页面布局时遇到了这个错误。该代码使用从 Enterprise Wiki 站点模板创建的站点在 VM 服务器上运行。但是在开发服务器上运行时,代码遇到了各种异常。
为了解决这个错误,我用三种不同的方式编写了这个类。这三个都在 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;
}