Add and Remove Site Columns from Site Content Type

In this blog post, I am going to add and remove site columns in existing site content type.

Content types empower you to arrange, oversee, and handle content reliably across your sites. To view all content types used in the site collection.
Open SharePoint site
Navigate to site settings ->Under Web designer galleries -> Click Site content type or navigate to this url “<Site Url>+ /_layouts/15/mngctype.aspx”

We can view all site contents type. To view details of the content type just click on the content type.

In this example, I am going to add and remove site columns used in Custom Content Types.
Here in this content type, there are two site columns Title, Contact is present. Through our code, we will remove the Contact site column and add a new site column to this content type.

img2

The script as follows:-

[code lang=”c”]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using System.IO;

namespace AddandRemoveFieldsFromCtype
{
class Program
{
static void Main(string[] args)
{
ClientContext ctx = new ClientContext(“http://portal/sites/site1”);
Web web = ctx.Web;
var password = “Password”;
SecureString secureString = new SecureString();
foreach (char c in password.ToCharArray()) secureString.AppendChar(c);

ctx.Credentials = new SharePointOnlineCredentials(“user@domain.com”, secureString);
ContentTypeCollection ctypecoll = web.ContentTypes;
ctx.Load(ctypecoll);
ctx.ExecuteQuery();
Field fieldToAdd = web.Fields.GetByTitle(“City”);

foreach (ContentType ctype in ctypecoll)
{
if (ctype.Name == “docCtype”)
{
FieldLinkCreationInformation fieldLinksCreation = new FieldLinkCreationInformation();
fieldLinksCreation.Field = fieldToAdd;
ctype.FieldLinks.Add(fieldLinksCreation);
ctype.Update(true); // adding site columns to content type
web.Update();

Field fieldToRemove = ctype.Fields.GetByTitle(“Contact”);
ctx.Load(fieldToRemove);
ctx.ExecuteQuery();
var fieldToRemoveId = fieldToRemove.Id;
FieldLinkCollection fieldLinkCollection = ctype.FieldLinks;
ctx.Load(fieldLinkCollection);
ctx.ExecuteQuery();
foreach (FieldLink fieldLink in fieldLinkCollection)
{
var fieldLinkId = fieldLink.Id;
if (fieldToRemoveId == fieldLinkId)
{
fieldLink.DeleteObject();
ctype.Update(false); // removing site columns from content type
web.Update();
}

}

ctx.ExecuteQuery();

}
}
}
}
}

[/code]

After code executed successfully you can manually navigate to that custom content type, you can find the existing site column is removed and new site columns is added to the content type.

img3

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *