Sharepoint - 由于ListId属性

时间:2015-04-30 13:33:27

标签: visual-studio list sharepoint web-parts

这是我的问题:

在SharePoint上,我创建了一个CQWP / ListViewWebPart' connected'到列表。使用此WP导入Visual Studio,我需要动态更改ListGuid,因为列表创建会在每个部署中创建一个新的ListGuid。使用Truez答案,我设法使CQWP工作

 ContentByQueryWebPart cqwp = wp as ContentByQueryWebPart;
 cqwp.ListGuid = list.ID.ToString();
 mgr.SaveChanges(cqwp);

 <property name="ListGuid" type="string">2c3e1e58-4fca-4b0e-a982-ffe5ac770ae4</property>

但是对于ListViewWebPart,使用相同的想法会抛出错误

XsltListViewWebPart xslt = wp as XsltListViewWebPart;
xslt.ListId = list.ID;
mgr.SaveChanges(xslt);

<property name="ListId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">68296faf-1aac-40c0-8d35-42bd3e12ec3b</property>

System.Reflection.TargetInvocationException - &gt;列表不存在 (使用调试器列表不是空的,我确实在xslt.ListId = list.ID中获取了它的ID;

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我以前使用代码将网页部件添加到页面中,这使您可以更灵活地更改和更新上述属性。

我写这个的原因是因为我们在列表设置中动态生成视图因此我们在给定时间没有ID,但是视图已存在!

希望这有帮助

 /// <summary>
    /// Adds list view webpart dynaically to the page.
    /// </summary>
    /// <param name="web">A Web object</param>
    /// <param name="filepath">The full URL of the page you want to add the web part to.param>
    /// <param name="listdisplayname">The display name of the list that the web part needs to reference</param>
    /// <param name="viewname">The display name of the view  the web part needs to look at.</param>
    /// <param name="xsllinkurl">The relative URL to the XLT file to apply.</param>
    /// <param name="zoneidname">The name of the zone to add the webpart to.</param>
    /// <param name="zoneIndex">The index to add the webpart.</param>
    private static void AddListViewWebPartToPage(SPWeb web, string filepath, string listdisplayname, string viewname, string xsllinkurl, string zoneidname, int zoneIndex)
    {
        SPList list = web.Lists.TryGetList(listdisplayname);

        SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager(filepath, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

        XsltListViewWebPart wp = new XsltListViewWebPart();
        wp.ID = Guid.NewGuid().ToString("B");
        wp.ListName = list.ID.ToString("B").ToUpper();
        wp.ViewGuid = list.Views[viewname].ID.ToString("B").ToUpper();
        wp.XslLink = xsllinkurl;
        wp.ChromeType = PartChromeType.None;

        wpm.AddWebPart(wp, zoneidname, zoneIndex);

    }

我相信只需将列表名称设置为ID即可使用上述代码。

干杯 Truez