sharepoint-2010 - SharePoint 2010中对LimitedWebPartManager.AddWebPart的调用中的"The specified view is invalid"

标签 sharepoint-2010

该代码曾经在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应该是什么意思,而我的想法已经用完了。其他人有吗?

最佳答案

要使其工作,只需执行以下操作:

  • 不要设置listviewwebpart对象的viewguid属性(将其留空)
  • 调用AddWebpart方法

  • 它将生成与新的隐藏 View 关联的新viewguid。
    然后,如果要自定义此 View ,请从列表中检索它并对其进行自定义。

    关于sharepoint-2010 - SharePoint 2010中对LimitedWebPartManager.AddWebPart的调用中的"The specified view is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768411/

    相关文章:

    javascript - 根据共享点新项目中的下拉选择隐藏字段

    sharepoint-2010 - SharePoint 自定义功能区操作中的 {ItemId} 为 null

    asp.net - sharepoint wiki 页面中的可编辑标题栏

    javascript - 访问/处理每个(嵌套)对象、数组或 JSON

    c# - 访问 SharePoint 2010 ListItem 中的属性以获取版本历史记录

    sharepoint - 在 SharePoint 中更改 Web.Config 时出现问题

    c# - 如何在 SharePoint 2010 中设置 SPField 的内部名称?

    powershell - 从站点的所有列表中检索所有项目-Powershell

    sharepoint-2010 - Sharepoint 服务器的 Java api

    c# - 在 Sharepoint 2010 中发送带附件的电子邮件