Written by

Vignesh

Share

  

Category

see all categories
intranet as a service

How to get and delete all site columns and content types from your SharePoint Site using C#

In this article, I am going to provide steps to get all the site columns and content types from your SharePoint Site and delete the site columns and content types using C# in Visual Studio 2017.

Step 1: Create Project for implementation.

1. Create a new project in the “Microsoft Visual Studio”.

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

Step 2: Install Packages for the Project

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

If you want to get from SharePoint 2013 then install “SharePointPnPCore2013” else if you would like to get from SharePoint Online then install “SharePointPnPCoreOnline” from “NuGet Package Manager”.

Step 3: Code Implementation

 If you want to get Content Types and Site Columns 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;

using System.Security;

namespace ConsoleApp

{

    class Program

    {

        static void 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();

}

Intranet as service

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. The console window pops up which displays the Content Types and Site Columns of the site.

With this we come to the end of this instructional blog. We hope you learnt how to get and delete Site Columns and Content Types in SharePoint using C#. 

Vignesh

Written by Vignesh