该代码曾经在FeatureReceiver.FeatureActivated中的WSS 3.0/MOSS 2007中工作:
using (SPLimitedWebPartManager limitedWebPartManager = Site.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared)) {
ListViewWebPart listViewWebPart = new ListViewWebPart {
Title = title,
ListName = list.ID.ToString("B").ToUpper(),
ViewGuid = view.ID.ToString("B").ToUpper()
};
limitedWebPartManager.AddWebPart(listViewWebPart, zone, position);
}
我正在尝试转换为SharePoint 2010,但现在失败,并显示以下信息:
System.ArgumentException: The specified view is invalid.
at Microsoft.SharePoint.SPViewCollection.get_Item(Guid guid)
at Microsoft.SharePoint.WebPartPages.ListViewWebPart.EnsureListAndView(Boolean requireFullBlownViewSchema)
at Microsoft.SharePoint.WebPartPages.ListViewWebPart.get_AppropriateBaseViewId()
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)
at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)
有趣的是,当我通过单元测试运行它时,它只能在FeatureActivated中失败。当我用Reflector调试时,它在此行上失败:
this.view = this.list.LightweightViews[new Guid(this.ViewGuid)];
list.LightweightViews仅返回一个 View ,即默认 View ,即使list.Views返回所有 View 。从单元测试运行时,LightweightViews返回我的所有 View 。我不知道LightweightViews应该是什么意思,而我的想法已经用完了。其他人有吗?
最佳答案
要使其工作,只需执行以下操作:
它将生成与新的隐藏 View 关联的新viewguid。
然后,如果要自定义此 View ,请从列表中检索它并对其进行自定义。
关于sharepoint-2010 - SharePoint 2010中对LimitedWebPartManager.AddWebPart的调用中的"The specified view is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768411/