Create List Content Type And Its Field Using CSOM

In this article, we are going to discuss an important and useful feature of SharePoint; i.e., Content Type. As we all know, the content type is the set of columns that we can reuse in all lists and libraries in a site. We are going to create a Content List and its column and will add this content type to all custom lists present in the site. Follow the below steps and code snippets to perform the above action.
 
Step 1
 
Set the credential by giving input of user ID and Password for SharePoint.
 
Step 2
 
Get the web ysing client context.
 
Step 3
 
The function CreateSpContentType () is used for the creation of List content type using the unique ID & Group. Here the title of the content type is set as “Employee Info”.
 
Step 4
 
The function CreateSiteField () is used then to create the fields of the content type which will represent the employee details. First, we must create site columns & then we can add it to the content type fields.
 
Step 5
 
After Creating the content type and its field, the function AddContentType () is used to attach the content type with all available custom lists in the site.
 
In this function, first we have to get all custom lists present in the site by using the base id (for custom List the Base Id=100) & then for adding the content type into the list we have to get the content type by using its unique ID which we used previously while creating the content type. (The Property ContentTypesEnabled should set to be true). 
  1. using System;  
  2. using System.Security;  
  3. using Microsoft.SharePoint.Client;  
  4. namespace Createcontenttype {  
  5.     class Program {  
  6.         static void Main(string[] args) {  
  7.             #region[Variables]  
  8.             string username = string.Empty;  
  9.             string password = string.Empty;  
  10.             #endregion  
  11.             #region[SP Variables]  
  12.             Web spWeb = null;  
  13.             ContentType contentType = null;  
  14.             #endregion  
  15.             using(ClientContext context = new ClientContext(https: //sharepoint.com/site)) //Enter the site url  
  16.                     {  
  17.                         SecureString securstr = new SecureString();  
  18.                         password = "1234@abc"//Enter the password  
  19.                         foreach(char ch in password) {  
  20.                             securstr.AppendChar(ch);  
  21.                         }  
  22.                         context.Credentials = new SharePointOnlineCredentials("user.onmicrosoft.com", securstr); //Enter the user id  
  23.                         spWeb = context.Site.RootWeb;  
  24.                         context.Load(spWeb);  
  25.                         context.Load(context.Web);  
  26.                         contentType = CreateSpContentType(context, spWeb, contentType);  
  27.                         CreateSiteField(context, spWeb, contentType);  
  28.                         AddContentType(context, spWeb);  
  29.                         Console.WriteLine("Success");  
  30.                         Console.ReadLine();  
  31.                     }  
  32.                 }  
  33.                 private static ContentType CreateSpContentType(ClientContext context, Web spWeb, ContentType contentType) {  
  34.                     try {  
  35.                         contentType = spWeb.ContentTypes.Add(new ContentTypeCreationInformation {  
  36.                             Name = "Employee Info",  
  37.                                 Id = "0x0100A33D9AD9805788419BDAAC2CCB37509E",  
  38.                                 Group = "List Content Types"  
  39.                         });  
  40.                         context.ExecuteQuery();  
  41.                     } catch (Exception e) {  
  42.                         Console.WriteLine(e.Message);  
  43.                     }  
  44.                     return contentType;  
  45.                 }  
  46.                 private static void CreateSiteField(ClientContext context, Web spWeb, ContentType contentType) {  
  47.                     #region[Variables]  
  48.                     string[] trgtfld = {  
  49.                         "EmployeeName",  
  50.                         "EmpAddress",  
  51.                         "EmpCity",  
  52.                         "JoinDate",  
  53.                         "EmpGender"  
  54.                     };  
  55.                     FieldCollection fields = null;  
  56.                     FieldLinkCreationInformation fldLink = null;  
  57.                     Field txtFld = null;  
  58.                     string TxtFieldAsXML = string.Empty;  
  59.                     Field addFld = null;  
  60.                     string addFieldAsXML = string.Empty;  
  61.                     string cityFieldAsXML = string.Empty;  
  62.                     Field cityFld = null;  
  63.                     string jdFieldAsXML = string.Empty;  
  64.                     Field jdFld = null;  
  65.                     string genFieldAsXML = string.Empty;  
  66.                     Field genFld = null;  
  67.                     #endregion  
  68.                     try {  
  69.                         fields = spWeb.Fields;  
  70.                         context.Load(fields);  
  71.                         //Adding site column to site  
  72.                         TxtFieldAsXML = @ "<Field Name='EmployeeName' DisplayName='Employee Name' Type='Text' Hidden='False' Group='EmployeeData' />";  
  73.                         txtFld = fields.AddFieldAsXml(TxtFieldAsXML, true, AddFieldOptions.DefaultValue);  
  74.                         addFieldAsXML = @ "<Field Name='EmpAddress' DisplayName='EmpAddress' Type='Note' Hidden='False' Group='EmployeeData' />";  
  75.                         addFld = fields.AddFieldAsXml(addFieldAsXML, true, AddFieldOptions.DefaultValue);  
  76.                         cityFieldAsXML = @ "<Field Name='EmpCity' DisplayName='EmpCity' Type='Text' Hidden='False' Group='EmployeeData' />";  
  77.                         cityFld = fields.AddFieldAsXml(cityFieldAsXML, true, AddFieldOptions.DefaultValue);  
  78.                         jdFieldAsXML = @ "<Field Name='JoinDate' DisplayName='JoinDate' Type='DateTime' Hidden='False' Group='EmployeeData' />";  
  79.                         jdFld = fields.AddFieldAsXml(jdFieldAsXML, true, AddFieldOptions.DefaultValue);  
  80.                         genFieldAsXML = @ "<Field Name='EmpGender' DisplayName='EmpGender' Type='Choice' Hidden='False' Group='EmployeeData' Format='Dropdown'>" + "<CHOICES>" + "<CHOICE>Male</CHOICE>" + "<CHOICE>Female</CHOICE>" + "</CHOICES>" + "</Field>";  
  81.                         genFld = fields.AddFieldAsXml(genFieldAsXML, true, AddFieldOptions.DefaultValue);  
  82.                         context.Load(fields);  
  83.                         foreach(var fld in trgtfld) {  
  84.                             try {  
  85.                                 contentType = spWeb.ContentTypes.GetById("0x0100A33D9AD9805788419BDAAC2CCB37509E");  
  86.                                 fldLink = new FieldLinkCreationInformation();  
  87.                                 fldLink.Field = context.Web.AvailableFields.GetByInternalNameOrTitle(fld);  
  88.                                 contentType.FieldLinks.Add(fldLink);  
  89.                                 contentType.Update(false);  
  90.                             } catch (Exception e) {  
  91.                                 Console.WriteLine(e);  
  92.                             }  
  93.                             context.ExecuteQuery();  
  94.                         }  
  95.                     } catch (Exception e) {  
  96.                         Console.WriteLine(e);  
  97.                     }  
  98.                 }  
  99.                 private static void AddContentType(ClientContext context, Web spWeb) {  
  100.                     #region[Variables]  
  101.                     ListCollection spListColl = null;  
  102.                     ContentType spContentType = null;  
  103.                     #endregion  
  104.                     try {  
  105.                         spListColl = spWeb.Lists;  
  106.                         context.Load(spListColl);  
  107.                         context.ExecuteQuery();  
  108.                         foreach(List list in spListColl) {  
  109.                             try {  
  110.                                 if (list.BaseTemplate == 100) {  
  111.                                     list.ContentTypesEnabled = true;  
  112.                                     spContentType = spWeb.ContentTypes.GetById("0x0100A33D9AD9805788419BDAAC2CCB37509E");  
  113.                                     list.ContentTypes.AddExistingContentType(spContentType);  
  114.                                     list.Update();  
  115.                                     spWeb.Update();  
  116.                                     context.ExecuteQuery();  
  117.                                 }  
  118.                             } catch (Exception e) {  
  119.                                 Console.WriteLine(e);  
  120.                             }  
  121.                         }  
  122.                     } catch (Exception e) {  
  123.                         Console.WriteLine(e);  
  124.                     }  
  125.                 }  
  126.             }  
  127.         }  
After running the code, we can check the content type in Site content type option under site setting. Then clicking on the content type title (Employee Info), we can check the field of content type. Please refer to the below image for better reference.
 
Img: Content type created 
 
 
 
 
 
Img: Field of Content type created