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 content types. 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 and Title, Contact are present. Through our code, we will remove the Contact site column and add a new site column to this content type.
 
Add and Remove Site Columns from Site Content Type 
 
The script is as follows,
  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. using System.IO;  
  9. namespace AddandRemoveFieldsFromCtype {  
  10.     class Program {  
  11.         static void Main(string[] args) {  
  12.             ClientContext ctx = new ClientContext("http://portal/sites/site1");  
  13.             Web web = ctx.Web;  
  14.             var password = "Password";  
  15.             SecureString secureString = new SecureString();  
  16.             foreach(char c in password.ToCharArray()) secureString.AppendChar(c);  
  17.             ctx.Credentials = new SharePointOnlineCredentials("[email protected]", secureString);  
  18.             ContentTypeCollection ctypecoll = web.ContentTypes;  
  19.             ctx.Load(ctypecoll);  
  20.             ctx.ExecuteQuery();  
  21.             Field fieldToAdd = web.Fields.GetByTitle("City");  
  22.             foreach(ContentType ctype in ctypecoll) {  
  23.                 if (ctype.Name == "docCtype") {  
  24.                     FieldLinkCreationInformation fieldLinksCreation = new FieldLinkCreationInformation();  
  25.                     fieldLinksCreation.Field = fieldToAdd;  
  26.                     ctype.FieldLinks.Add(fieldLinksCreation);  
  27.                     ctype.Update(true); // adding site columns to content type  
  28.                     web.Update();  
  29.                     Field fieldToRemove = ctype.Fields.GetByTitle("Contact");  
  30.                     ctx.Load(fieldToRemove);  
  31.                     ctx.ExecuteQuery();  
  32.                     var fieldToRemoveId = fieldToRemove.Id;  
  33.                     FieldLinkCollection fieldLinkCollection = ctype.FieldLinks;  
  34.                     ctx.Load(fieldLinkCollection);  
  35.                     ctx.ExecuteQuery();  
  36.                     foreach(FieldLink fieldLink in fieldLinkCollection) {  
  37.                         var fieldLinkId = fieldLink.Id;  
  38.                         if (fieldToRemoveId == fieldLinkId) {  
  39.                             fieldLink.DeleteObject();  
  40.                             ctype.Update(false); // removing site columns from content type  
  41.                             web.Update();  
  42.                         }  
  43.                     }  
  44.                     ctx.ExecuteQuery();  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49. }  
After code is executed successfully, you can manually navigate to that custom content type, you can find that the existing site column is removed and new site columns are added to the content type.
 
Add and Remove Site Columns from Site Content Type