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”.
Choose Console App (.NET Framework) in the popup and then click OK to create the project.
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”.
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”.
Step 3 - Code Implementation
If you want to get the Content Types and Site Columns from SharePoint Online, follow the sample code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Security;
- namespace ConsoleApp {
- classProgram {
- staticvoid Main(string[] args) {
- GetSiteColumns_SharePoint("enter your username", " enter your password");
- GetSiteContentTypes_SharePoint("enter your username", " enter your password");
- }
- public static void GetSiteContentTypes_SharePoint(string username, string password) {
- using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {
- SecureString passWord = new SecureString();
- foreach(char c in pwd.ToCharArray()) passWord.AppendChar(c);
- clientcontext.Credentials = new SharePointOnlineCredentials(username, password);
- ContentTypeCollection displayContentTypes = clientcontext.Web.ContentTypes;
- clientcontext.Load(displayContentTypes);
- clientcontext.ExecuteQuery();
- foreach(ContentType ContentTypes in displayContentTypes) {
- Console.WriteLine(ContentTypes.Name);
- }
- Console.ReadLine();
- }
- }
- public static void GetSiteColumns_SharePoint(string username, string password) {
- using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {
- SecureString passWord = new SecureString();
- foreach(char c in pwd.ToCharArray()) passWord.AppendChar(c);
- clientcontext.Credentials = new SharePointOnlineCredentials(username, password);
- FieldCollection displayFields = clientcontext.Web.Fields;
- clientcontext.Load(displayFields);
- clientcontext.ExecuteQuery();
- foreach(Field fields in displayFields) {
- Console.WriteLine(fields.Name);
- }
- Console.ReadLine();
- }
- }
- }
If you want to delete content types, replace the following snippet from the above code.
- foreach(ContentType ContentTypes in displayContentTypes) {
- Console.WriteLine(ContentTypes.Name);
- ContentTypes.DeleteObject();
- clientcontext.ExecuteQuery();
- }
If you want to delete site columns, replace the following snippet from the above code.
- foreach(Field fields in displayFields) {
- Console.WriteLine(fields.Name);
- fields.DeleteObject();
- clientcontext.ExecuteQuery();
- }
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#.