How to Get And Delete All Site Columns And Content Types From SharePoint Site Using C#

In this blog, we are going to learn the process of fetching all the site columns and content types from a SharePoint website and then deleting them in bulk.

To check the entire site's columns and content types then delete them manually is difficult and takes a lot of time.  This blog helps you to automate the "Get and Delete" process for the entire site. 

Step 1 - Create a project for implementation

Create a new project in “Microsoft Visual Studio”.

How To Get And Delete All Site Columns And Content Types From SharePoint Site Using C#

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

How To Get And Delete All Site Columns And Content Types From SharePoint Site Using C#

Step 2 - Install packages for the Project

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

How To Get And Delete All Site Columns And Content Types From SharePoint Site Using C#

If you want to get from SharePoint 2013, then install “SharePointPnPCore2013”; otherwise, if you want to get from SharePoint Online, then install “SharePointPnPCoreOnline” from “NuGet Package Manager”.

How To Get And Delete All Site Columns And Content Types From SharePoint Site Using C# 

Step 3 - Code Implementation

If you want to get the Content Types and Site Columns from SharePoint Online, follow the sample code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using System.Security;  
  8. namespace ConsoleApp {  
  9.     classProgram {  
  10.         staticvoid Main(string[] args) {  
  11.             GetSiteColumns_SharePoint("enter your username"" enter your password");  
  12.             GetSiteContentTypes_SharePoint("enter your username"" enter your password");  
  13.         }  
  14.         public static void GetSiteContentTypes_SharePoint(string username, string password) {  
  15.             using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {  
  16.                 SecureString passWord = new SecureString();  
  17.                 foreach(char c in pwd.ToCharArray()) passWord.AppendChar(c);  
  18.                 clientcontext.Credentials = new SharePointOnlineCredentials(username, password);  
  19.                 ContentTypeCollection displayContentTypes = clientcontext.Web.ContentTypes;  
  20.                 clientcontext.Load(displayContentTypes);  
  21.                 clientcontext.ExecuteQuery();  
  22.                 foreach(ContentType ContentTypes in displayContentTypes) {  
  23.                     Console.WriteLine(ContentTypes.Name);  
  24.                 }  
  25.                 Console.ReadLine();  
  26.             }  
  27.         }  
  28.         public static void GetSiteColumns_SharePoint(string username, string password) {  
  29.             using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {  
  30.                 SecureString passWord = new SecureString();  
  31.                 foreach(char c in pwd.ToCharArray()) passWord.AppendChar(c);  
  32.                 clientcontext.Credentials = new SharePointOnlineCredentials(username, password);  
  33.                 FieldCollection displayFields = clientcontext.Web.Fields;  
  34.                 clientcontext.Load(displayFields);  
  35.                 clientcontext.ExecuteQuery();  
  36.                 foreach(Field fields in displayFields) {  
  37.                     Console.WriteLine(fields.Name);  
  38.                 }  
  39.                 Console.ReadLine();  
  40.             }  
  41.         }  
  42.     } 

If you want to delete content types, replace the following snippet from the above code.

  1. foreach(ContentType ContentTypes in displayContentTypes) {  
  2.     Console.WriteLine(ContentTypes.Name);  
  3.     ContentTypes.DeleteObject();  
  4.     clientcontext.ExecuteQuery();  
  5. }  

If you want to delete site columns, replace the following snippet from the above code.

  1. foreach(Field fields in displayFields) {  
  2.     Console.WriteLine(fields.Name);  
  3.     fields.DeleteObject();  
  4.     clientcontext.ExecuteQuery();  
  5. }  

Step 4 - Run the project

Click “Start” to run the project. In the console window, it displays the Content Types and Site Columns of the site.

I hope you learned how to get and delete Site Columns and Content Types in SharePoint using C#.