Deleting Specific Listing Using CSOM Through Console Application

In one of our SharePoint online projects, we are creating multiple lists and associating our custom specific content type to those. But somehow the lists are not getting created properly and most of those are corrupted. For some lists, content types are not associated. We were doing all these operations using JavaScript Object Model (JSOM). In this case, we had to delete all these lists and then create them again. Here, we have the option of either using PowerShell script or CSOM. In this scenario, I always prefer CSOM since it is easier to implement as compared to PowerShell.

Here, the code is pretty simple.

Steps for deleting the list using CSOM and console application,

  1. Create C# console application in Visual Studio. Here, I am using Visual Studio 2017 community edition.

  1. Add the reference to following two client DLLs,

    1. SharePoint.Client.dll
    2. SharePoint.Client.Runtime.dll

To add these references, we need to install SharePoint Online Client Components SDK .

  1. Connect to O365,

    1. We are storing our SharePoint online site URL, user name, and password in configuration file. Get the details to connect O365.
      1. //Read the details from config file – App.config  
      2. string siteURL = ConfigurationSettings.AppSettings["siteURL"];  
      3. string userName = ConfigurationSettings.AppSettings["userName"];  
      4. string password = ConfigurationSettings.AppSettings["password"];  
    2. Create the client context object and set the credentials.
      1. ClientContext clientContext = new ClientContext(siteURL);  
      2. SecureString securePassword = new SecureString();  
      3. foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);  
      4. clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);  
  1. Load the web object and get all lists.
    1. //Load the web  
    2. Web web = clientContext.Web;  
    3.    clientContext.Load(web);  
    5.    //Get all lists  
    6.    ListCollection allList = web.Lists;    
    7.   clientContext.Load(allList);  
    9.   clientContext.ExecuteQuery();  

  1. The next step is to find the lists which we need to delete.
    1. //Get all the lists whose content type is "MyContentType" and store in one array list  
    2. ArrayList listArray = new ArrayList();  
    4. //Below is the content type id of which we need to find the lists and delete those  
    5. string myContentTypeID = "0x01007931F4F9F69E0443972158D57498131A00A96BC22822FD7D40817DBAB3343D80AA";  
    6. //Loop through all the lists and check for their content type ids  
    7.             foreach(List _list in allList)  
    8.             {  
    9.                 //Get all associated content types to the list  
    10.                 ContentTypeCollection ctCollection = _list.ContentTypes;  
    12. //Find our content type - Here, important point is even though our content   type is not associated, it dosent return null  
    13. //It returns new CT object but "ServerObjectIsNull" property returns false in case content type is not associated.   
    14.                 ContentType ct = _list.ContentTypes.GetById(myContentTypeID);  
    15.                 clientContext.Load(ct);  
    16.                 clientContext.ExecuteQuery();  
    18.                 bool? isCT = ct.ServerObjectIsNull;  
    20.  //Check if our content type with given id is associated or not if(isCT.ToString().ToLower().Equals("false",  
    21. StringComparison.InvariantCultureIgnoreCase))  
    22.                 {  
    23.                     listArray.Add(_list.Title);  
    24.                 }  
    25. }//foreach(List _list in allList)  
    26. //Now loop through all the array and find the list from above list collection object and call "DeleteObject" method  
    27.             for (int i = 0; i < listArray.Count; i++)  
    28.             {  
    29.                     string listTitle = listArray[i].ToString();  
    30.                     Console.WriteLine("List Deleting - :" + listTitle);  
    31.                     allStudentList.GetByTitle(listArray[i].ToString()).DeleteObject();  
    32.                     Console.WriteLine("List deleted successfully " + listTitle);  
    33.                     web.Update();  
    34.                     clientContext.ExecuteQuery();  
    35.   } // for (int i = 0; i < listArray.Count; i++)  


As usual, comments / suggestions / feedback / questions are always welcome!


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now