Written by

Vignesh

Share

  

Category

see all categories
intranet as a service

How to Remove All Document Libraries from SharePoint Site Using C#

This article provides steps to remove all document libraries from SharePoint Site using C# in Visual Studio 2017. This will help you implement the deletion process for the entire site. 

Step 1: Create Project for implementation. 

 Create a new project in “Microsoft Visual Studio”. 

 Create new project in Microsoft Visual Studio

Choose Console App (.NET Framework) in the popup and then click ok to create the project. 

Create ConsoleApp

Step 2: Install Packages for the Project 

Go to “Tools”, click on “NuGet Package Manager” from the Tools and choose “Manage NuGet Packages for solution ” in the “NuGet Package Manager”. 

Manage NuGet Packages for solution

If you want to delete libraries from SharePoint 2013 then install “SharePointPnPCore2013; if you want to take care of the deletion process from SharePoint Online then install “SharePointPnPCoreOnline” from “NuGet Package Manager”. 

SharePointPnPCore2013

Step 3: Code Implementation. 

 If you want to delete libraries from SharePoint Online, follow the below sample code. 

  using (ClientContext clientcontext = new ClientContext("https://SiteUrl")) 
            { 
                clientContext.Credentials = new SharePointOnlineCredentials(username, password); 
                clientcontext.Load(clientcontext.Web.Lists); 
                clientcontext.ExecuteQuery(); 
                var lists = clientcontext.Web.Lists; 
                foreach (List list in lists.ToList()) 
                { 
                    if (list.BaseType.ToString() == "DocumentLibrary") 
                        {                            
                                Console.WriteLine(list.Title); 
                                list.DeleteObject(); 
                                clientcontext.ExecuteQuery();                                             
                    } 
                } 
            } 

If you want to delete libraries from SharePoint 2013, follow the below sample code. 

using (ClientContext clientcontext = new ClientContext("https://SiteUrl ")) 
            { 
                clientcontext.Credentials = new NetworkCredential(user,pwd); 
                clientcontext.Load(clientcontext.Web.Lists); 
                clientcontext.ExecuteQuery(); 
                var lists = clientcontext.Web.Lists; 
                foreach (List list in lists.ToList()) 
                { 
                    if (list.BaseType.ToString() == "DocumentLibrary") 
                        {                            
                                Console.WriteLine(list.Title); 
                                list.DeleteObject(); 
                                clientcontext.ExecuteQuery();                                             
                    } 
                } 
            } 

If you want to skip a library from deletion process. Make change like the following code to skip the particular library from the deletion. 

  if (list.BaseType.ToString() == "DocumentLibrary" && list.Title != "Important Library Name") 
                        {                            
                                Console.WriteLine(list.Title); 
                                list.DeleteObject(); 
                                clientcontext.ExecuteQuery();                                             
                    }

Step:4 Run the Project. 

Click “Start” to run the project. The name of the library which going to delete that displays in the console. 

Learn how to Install SharePoint PnP Core library in Visual Studio 2017. 

We hope you learnt how you can remove all document libraries in SharePoint using C#. 

Vignesh

Written by Vignesh