Sharepoint “中引发的NullReferenceException”;GetAvailablePageLayouts“;
我最近在尝试从子网站的自定义版面页面获取发布网站(Sharepoint “中引发的NullReferenceException”;GetAvailablePageLayouts“;,sharepoint,sharepoint-2010,page-layout,publishing-site,Sharepoint,Sharepoint 2010,Page Layout,Publishing Site,我最近在尝试从子网站的自定义版面页面获取发布网站(Microsoft.SharePoint.publishing.PublishingWeb)中的所有页面版面时遇到此错误。代码使用从企业Wiki站点模板创建的站点在VM服务器上运行。但是在开发服务器上运行时,代码遇到了各种异常 为了避免这个错误,我用三种不同的方式对类进行编码。这三个都在VM中工作,但在开发服务器中使用时,这三个都引发了一个异常。例如: private PageLayout FindPageLayout(PublishingWeb
Microsoft.SharePoint.publishing.PublishingWeb
)中的所有页面版面时遇到此错误。代码使用从企业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;
}
私有页面布局FindPageLayout(PublishingWeb pubWeb,string examplePage)
{
/*错误发生在这个方法中*/
if(pubWeb==null)
抛出新ArgumentException(“pubWeb参数不能为null”);
PublishingSite pubSiteCollection=新的PublishingSite(pubWeb.Web.Site);
字符串pageLayoutName=“EnterpriseWiki.aspx”;//用于测试目的
PageLayout=null;
/*选项1:从集合中获取页面布局,布局名称用作索引
*结果:GetPageLayouts()中的System.NullReferenceException
*/
layout=pubSiteCollection.PageLayouts[“/”目录/母版页/“+pageLayoutName];
/*选项2:调出现有发布页面,然后使用layout属性查找该页面的布局
*结果:函数COM异常不正确
*/
SPListItem=pubWeb.Web.GetListItem(examplePage);
PublishingPage项目=PublishingPage.GetPublishingPage(listItem);
布局=项目。布局;
/*选项3:调用“GetPageLayouts”并遍历结果,查找特定名称的布局
*结果:从Microsoft.SharePoint.Publishing.PublishingSite.GetPageLayouts()引发System.NullReferenceException异常
*/
PageLayoutCollectionLayouts=pubSiteCollection.GetPageLayouts(true);
对于(int i=0;i
这是我在大约一周后找到的解决方案:
确保在调用PublishingWeb.GetPublishingWeb(SPWeb)方法获取“PublishingWeb”对象时,将已完全检索的SPWeb对象传递给该对象。更具体地说,我会确保在任何网站上调用SPSite.OpenWeb,如下所示:
using (SPSite site = new SPSite(folder.ParentWeb.Url))
{
SPWeb web = site.OpenWeb();
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
/* work with PublishingWeb here ... */
web.Close();
}
一旦我做了这个简单的更改,问题中提到的所有错误都被清除了,无论在什么上下文中我称之为“GetPageLayouts”或“GetAvailablePageLayouts”。这句话的意思是:
使用此方法可以访问网站的PublishingWeb行为
已检索的SPWeb类的实例