1

如何以编程方式删除 SharePoint 列表视图?

MyCustomView:是我以编程方式创建的自定义视图。我想删除以相同名称创建的所有视图

using (SPSite oSPsite = new SPSite("http://xxxxxxxxxx:20000/sites/myWA/test"))
                {                    
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        SPList oTransDataList = oSPWeb.Lists["MyDataList"];
                        oSPWeb.AllowUnsafeUpdates = true;                        
                        SPViewCollection oViewCollection = oTransDataList.Views;
                        int i = 1;
                        foreach (SPView oViewColl in oViewCollection)
                        {
                            if (oViewColl.Title == "MyCustomView")
                                {
                                    oViewCollection.Delete(oViewColl.ID);

                                    //oTransDataList.Views.Delete(oViewColl.ID); 
                                    oTransDataList.Update();

                                }
                        }
                    }
                }

我注意到SPViewCollection oViewCollection = oTransDataList.Views;仅包含 1 个视图。我可以知道为什么会发生这种情况,我有 10 多个视图,其中 9 个视图是自定义的同名视图。IE。我的自定义视图

4

2 回答 2

3

看起来你在正确的轨道上。但是,我建议分两步执行此操作。首先,收集所需的视图。二是删除意见。合并这些步骤的问题在于,一旦删除视图,您正在循环的集合就会发生变化。

using (SPSite oSPsite = new SPSite("http://xxxxxxxxxx:20000/sites/myWA/test"))
{                    
    using (SPWeb oSPWeb = oSPsite.OpenWeb())
    {
        SPList oTransDataList = oSPWeb.Lists["MyDataList"];
        oSPWeb.AllowUnsafeUpdates = true;                        
        List<Guid> ids = new List<Guid>();
        SPViewCollection oViewCollection = oTransDataList.Views;
        foreach (SPView oViewColl in oViewCollection)
        {
            if (oViewColl.Title == "MyCustomView")
            {
                ids.Add(oViewColl.ID);
            }
        }
        foreach (Guid id in ids)
        {
            oViewCollection.Delete(id);
        }
    }
}

作为另一种选择,如果您向后逐步浏览集合,您可能可以组合这些步骤:

for (int i = oViewCollection.Count - 1; i >= 0; --i)
{
    SPView oViewColl = oViewCollection[i];
    if (oViewColl.Title == "MyCustomView")
    {
        oViewCollection.Delete(oViewColl.ID);
    }
}
于 2013-10-15T14:26:50.913 回答
0

我已经用相同的名称以编程方式创建了 10 次视图,并测试它显示了 10 个视图 SPViewCollection oViewCollection = oTransDataList.Views。

 using (SPSite oSPsite = new SPSite("http://SampletestSite.com/Trial"))
        {
            oSPsite.AllowUnsafeUpdates = true;

            using (SPWeb oSPWeb = oSPsite.OpenWeb())
            {
                oSPWeb.AllowUnsafeUpdates = true;
                SPList list = oSPWeb.Lists["Sample"];
               StringCollection strViewFields = new StringCollection();
                    strViewFields.Add("Title");
                    strViewFields.Add("FirstName");
                    strViewFields.Add("LastName");

            // create a standard view with the set of fields defined in the collection
               list.Views.Add("SampleTest", strViewFields, String.Empty,
                        100, true, false, SPViewCollection.SPViewType.Html, false);

               list.Update();

         oSPWeb.AllowUnsafeUpdates = false;
    }

oSPsite.AllowUnsafeUpdates = false;
 }

在下面的代码注释中,尝试执行
oTransDataList.Views.Delete(oViewColl.ID); // 给出错误 id not matching Add watch for oViewCollection.Count 并检查,你甚至可以通过添加 watch for oViewCollection[index] 来验证标题

于 2013-10-15T10:22:18.630 回答