“GetAvailablePageLayouts”中抛出 NullReferenceException
最近,当我尝试从子网站中的自定义布局页面获取发布网站 (Microsoft.SharePoint.Publishing.PublishingWeb
) 中的所有页面布局时,遇到了此错误。该代码在使用从 Enterprise Wiki 站点模板创建的站点的 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;
}
I ran into this error recently when trying to get all the page layouts in a publishing site (Microsoft.SharePoint.Publishing.PublishingWeb
) from a custom layouts page in a subsite. The code worked on a VM server using a site created from the Enterprise Wiki site template. But when running on the development server the code encountered various exceptions.
Attempting to get around the error, I coded the class three different ways. All three worked in the VM, but all three threw an exception when used in the development server. Example:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我在一周左右找到的解决方案:
确保当您通过调用 PublishingWeb.GetPublishingWeb(SPWeb) 方法来获取“PublishingWeb”对象时,您正在向其中传递一个已完全检索到的 SPWeb 对象。更具体地说,我将确保在任何网站上调用 SPSite.OpenWeb,如下所示:
一旦我进行了这个简单的更改,问题中提到的所有错误都被清除,无论我在什么上下文中调用“GetPageLayouts”或“GetAvailablePageLayouts” ”。 方法文档说了这一点,它的真正含义是:
Here was the solution I found after a week or so:
Make sure that when you are getting your "PublishingWeb" object by calling that PublishingWeb.GetPublishingWeb(SPWeb) method that you are passing into it an SPWeb objec that has been fully retrieved. More specifically, I would make sure to call SPSite.OpenWeb on any website, as follows:
Once I made this simple change, all the errors mentioned in the question were cleared up, no matter in what context I called "GetPageLayouts" or "GetAvailablePageLayouts". The method documentation says this, and it really means it: