Creating Content Type And Field Using JSOM

In this blog we are going to discuss about how to create list content type and its field under site content type using JSOM. Content type is the set of columns which we can reuse in all lists and libraries in a site.
 
I am providing the steps below to create content type and its fields.
 
Step 1
 
First get the Current Context.
 
Step 2
 
We have to get the content type collection and then have to create a new content type by using "SP.ContentTypeCreationInformation". Here we must set the new content type name, description, ID, and group.
 
Step 3
 
Declare a function "createSfield()" for creating site field. Here I have created a text field having name "EmployeeName" and groupname "Employee Data" by using "addFieldAsXml". And called this function inside the executequery() of fuction "createCType()".
 
Step 4
 
Then we have added the created site field to the new content type. For this first we get the content type by its id then add the field using "SP.FieldLinkCreationInformation()".
  1. <html>  
  2.     <body>  
  3.         <script type="text/javascript">  
  4.          var contentTypeColl;  
  5.          function createCType() {  
  6.             var ctx = new SP.ClientContext.get_current();  
  7.             if (ctx != undefined && ctx != null) {  
  8.                var web = ctx.get_web();  
  9.                this.contentTypeColl = web.get_contentTypes();  
  10.                var newcType = new  
  11. SP.ContentTypeCreationInformation();  
  12.                newcType.set_name('My Content Type');  
  13.                newcType.set_description('My list content type');               newcType.set_id("0x0100A33D9AD9805788419BDAAC2CCB37509E");  
  14.                newcType.set_group("List Content Types");  
  15.                this.contentTypeColl.add(newcType);  
  16.                ctx.load(this.contentTypeColl);  
  17.                ctx.executeQueryAsync(function () {  
  18.                   createSfield(web, ctx);  
  19.                   var field = ctx.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("EmployeeName");  
  20.                   var cType = ctx.get_site().get_rootWeb().get_contentTypes().getById("0x0100A33D9AD9805788419BDAAC2CCB37509E");  
  21.                   ctx.load(cType);  
  22.                   ctx.load(field);  
  23.                   var fInfo = new  
  24. SP.FieldLinkCreationInformation();  
  25.                   fInfo.set_field(field);  
  26.                   var fLink = cType.get_fieldLinks().add(fInfo);  
  27.                   cType.update(true);  
  28.                   ctx.load(fLink);  
  29.                   ctx.executeQueryAsync(function () {  
  30.                      alert("Content Type and field created successfully");  
  31.                   }, function (sender, args) {  
  32.                      alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  33.                   });  
  34.                }, function (sender, args) {  
  35.                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  36.                });  
  37.             }  
  38.          }  
  39.          function createSfield(web, ctx) {  
  40.             var fields = web.get_fields();  
  41.             var fieldSchema = '  
  42.             <Field Type="Text" \Name = "EmployeeName" \DisplayName = "Employee Name" \Required = "TRUE" \Group = "Employee Data" /> ';  
  43.             fields.addFieldAsXml(fieldSchema);  
  44.             ctx.executeQueryAsync(Success, Fail);  
  45.          }  
  46.          function Success() {  
  47.             alert('SiteColumn is created successfully');  
  48.          }  
  49.          function Fail() {  
  50.             alert('site column Creation Failed');  
  51.          }  
  52.         </script>  
  53.         <input id="btnCreateContentType" onclick="createCType()" type="button" value="Create Content Type and field" />  
  54.     </body>  
  55. </html>  
After running the code, we can check the content type in the Site content type option under site setting. Then by clicking on the content type title (‘My Content Type’), we can check the field of content type. Please refer to the the below image for better reference.
 
Content type created in the below image.
 
Creating Content Type And Field Using JSOM
 
Field added to the content type in the below image.
 
Creating Content Type And Field Using JSOM