Deleting the entire document library in the site manually is difficult and takes more time to process. This blog will help you learn how to automate the deletion process for the entire site.
Step 1 - Create a project for implementation.
- Create a new project in “Microsoft Visual Studio”.
- Choose Console App (.NET Framework) in the pop-up and then click OK to create the project.
Step 2 - Install packages for the project
- Go to the “Tools”, click “NuGet Package Manager” from Tools, and choose “Manage NuGet Packages for solution ” in the NuGet Package Manager.
- If you want to delete the libraries from SharePoint 2013, then install “SharePointPnPCore2013”; else, you want to delete from SharePoint Online. Install “SharePointPnPCoreOnline”.
Step 3 - Code Implementation
If you want to delete the libraries from SharePoint Online, follow the below sample code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- namespace ConsoleApp {
- classProgram {
- staticvoid Main(string[] args) {
- DeleteLibraries_SharePointOnline("enter your username", " enter your password");
- }
- public static void DeleteLibraries_SharePointOnline(string username, string password) {
- using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {
- clientContext.Credentials = newSharePointOnlineCredentials(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 block a library from the deletion process, make changes like in the following code to skip that particular library.
- 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 libraries to be deleted get displayed in the console.
I hope you learned how to remove all document libraries in SharePoint using C#.