此代码曾用于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);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试转换为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)
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我从单元测试运行它时,它只能在FeatureActivated中失败.当我使用Reflector进行调试时,它在此行上失败:
this.view = this.list.LightweightViews[new Guid(this.ViewGuid)];
Run Code Online (Sandbox Code Playgroud)
list.LightweightViews只返回一个视图,默认视图,即使list.Views返回所有视图.从单元测试运行时,LightweightViews会返回我的所有视图.我不知道LightweightViews应该是什么意思,我的想法已经不多了.还有其他人吗?
小智 6
要使其工作,只需执行以下操作:
它将生成与新隐藏视图关联的新viewguid.然后,如果要自定义此视图,请从列表中检索并自定义它.
| 归档时间: |
|
| 查看次数: |
9849 次 |
| 最近记录: |