Wednesday, 31 May 2017

SharePoint online / Office 365 – Deleting specific lists using CSOM through console application

Hi All,
 
In this article I’ll share the detailed steps for deleting specific lists based on content type IDs using CSOM in console application.


Scenario: In one of our SharePoint online project, we are creating multiple lists and associating our custom specific content type to those. But somehow lists are not getting created properly and most of those are corrupted. For some list content types are not associated. We all these operations were doing using Java Script Object Model (JSOM). In this case we need to then delete all these lists and then create again. Here we have option either using PowerShell script or CSOM. In these scenario, I always prefer CSOM since which is easy as compare to PowerShell and easy to implement. 

Here code is pretty simple, nothing complex there but sharing since if required then some time will be saved J


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.

2.      Add the reference to following two client dlls

a.      Microsoft.SharePoint.Client.dll

b.      Microsoft.SharePoint.Client.Runtime.dll

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

3.      Connect to O365:

a.      We are storing our SharePoint online site URL, user name and password in configuration file. Get the details to connect O365 as,


           //Read the details from config file – App.config

       string siteURL = ConfigurationSettings.AppSettings["siteURL"];

       string userName = ConfigurationSettings.AppSettings["userName"];

       string password = ConfigurationSettings.AppSettings["password"];


b.      Create the client context object and set the credentials as


  ClientContext clientContext = new ClientContext(siteURL);

    SecureString securePassword = new SecureString();


     foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);


clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);


4.      Load web object and get all lists as

                     //Load the web

                     Web web = clientContext.Web;

   clientContext.Load(web);

   //Get all lists

   ListCollection allList = web.Lists;  

  clientContext.Load(allList);


  clientContext.ExecuteQuery();

5.      Next step is to find the lists which we need to delete

//Get all the lists whose content type is "MyContentType" and store in one array list

ArrayList listArray = new ArrayList();

//Below is the content type id of which we need to find the lists and delete those

string myContentTypeID = "0x01007931F4F9F69E0443972158D57498131A00A96BC22822FD7D40817DBAB3343D80AA";


//Loop through all the lists and check for their content type ids

            foreach(List _list in allList)

            {

                //Get all associated content types to the list

                ContentTypeCollection ctCollection = _list.ContentTypes;
   //Find our content type - Here, important point is even though our content   type is not associated, it dosent return null


//It returns new CT object but "ServerObjectIsNull" property returns false in case content type is not associated.

          ContentType ct = _list.ContentTypes.GetById(myContentTypeID);

          clientContext.Load(ct);

          clientContext.ExecuteQuery();


             bool? isCT = ct.ServerObjectIsNull;



 //Check if our content type with given id is associated or not if(isCT.ToString().ToLower().Equals("false",

StringComparison.InvariantCultureIgnoreCase))

                {

                    listArray.Add(_list.Title);

                }

}//foreach(List _list in allList)


//Now loop through all the array and find the list from above list collection object and call "DeleteObject" method


            for (int i = 0; i < listArray.Count; i++)

            {

                    string listTitle = listArray[i].ToString();

                    Console.WriteLine("List Deleting - :" + listTitle);

                    allList.GetByTitle(listArray[i].ToString()).DeleteObject();

                    Console.WriteLine("List deleted successfully " + listTitle);

                    web.Update();

                    clientContext.ExecuteQuery();

  } // for (int i = 0; i < listArray.Count; i++)


Thanks!


Enjoy Reading :)


As usual any comment / suggestions / feedback / questions always welcome :)

No comments: