簡體   English   中英

如何在C#,Sharepoint 2010中以編程方式刪除列表的視圖?

[英]How to delete a list's view programatically in C#, Sharepoint 2010?

如何以編程方式刪除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個視圖是自定義的同名。 即。 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;                        
        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);
    }
}

我已經使用相同的名稱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不匹配為oViewCollection.Count添加監視並檢查,你甚至可以通過為oViewCollection [index]添加監視來驗證標題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM