2013-10-15 57 views
1

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

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

+0

上面的代碼是正確答案了。我修改了正確的錯誤代碼(工作代碼) – Gaurravs

回答

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); 
    } 
} 
+0

嗨Rich,代碼與第二種方法,你已經提供了很好的工作.. 謝謝你這麼多的幫助 – Gaurravs

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不匹配 爲oViewCollection.Count添加監視並檢查,您甚至可以通過爲oViewCollection添加監視來驗證標題[index]

+0

嗨Suchithra,您提供的代碼是創建視圖,我不是在尋找。但是您在代碼下面提供的評論是有幫助的。 oTransDataList.Views.Delete(oViewColl.ID);實際上並不是必須的謝謝YOu太多的幫助 – Gaurravs

相關問題