Create and Delete SharePoint List, Document Library Programmatically using CSOM

Create and Delete SharePoint List, Document Library using CSOM Programmatically

Create and Delete SharePoint List, Document Library Programmatically using CSOM. In this post I described you how to Create and Delete SharePoint List, Document Library using CSOM programmatically. Document Library, SharePoint List can be created or delete as the manual to go on site content and remove List or library from SharePoint site. But when we need to create and delete Document Library or SharePoint List in bulk, Need to develop the code. So here is complete code to Create and Delete SharePoint List, Document Library using CSOM.

[AdSense-A]

public static void DeleteDocumentLibrary(string SiteCollectionURL, string DocumentLibraryName)
        {
            try
            {
               
                ClientContext clientContext = new ClientContext(SiteCollectionURL);
                ListCollection listCollection = clientContext.Web.Lists;
                clientContext.Load(listCollection, lsts => lsts.Include(list => list.Title).Where(list => list.Title == DocumentLibraryName));
                clientContext.ExecuteQuery();
                int isExist = listCollection.Count;
                if (isExist == 1)
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Document Library: " + DocumentLibraryName + " is being Deleted...");
                    List List = listCollection.GetByTitle(DocumentLibraryName);
                    clientContext.Load(List);
                    clientContext.ExecuteQuery();
                    List.DeleteObject();
                    clientContext.ExecuteQuery();
                    Console.WriteLine("Document Library: " + DocumentLibraryName + " successfully Deleted.");
                    ReadWriteException.LogFileWrite("Document Library " + DocumentLibraryName + " Deleted Successfully.");

                }
                else
                {
                    // Console.WriteLine("Document Library: " + DocumentLibraryName + " Not Dound.");
                    ReadWriteException.LogFileWrite("Document Library " + DocumentLibraryName + " Not Dound.");
                }

            }
            catch (Exception ex)
            {
                ReadWriteException.LogFileWrite(ex.Message);
                ReadWriteException.LogFileWrite(ex.InnerException.Message);
            }
        }

Create Document Library or List using csom

using (ClientContext clientContext = new ClientContext(url))
{
     ListCreationInformation ListCI = new ListCreationInformation();
     ListCI.Description = "This is My DocLibrary";
     ListCI.Title = "My Library";
     ListCI.TemplateType = 101;
     List newLib = clientContext.Web.Lists.Add(ListCI);
     clientContext.Load(newLib);
     clientContext.ExecuteQuery();
}

 


Comments

  1. how to compare Source site Library with Destination site library, and how to display those missing documents in excel sheet.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!