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.

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, group.

Step 3 : Declared 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 added the field using “SP.FieldLinkCreationInformation()”.

    <script type="text/javascript">
        var contentTypeColl;
        function createCType() {
            var ctx = new SP.ClientContext.get_current();
            if (ctx != undefined && ctx != null) {
                var web = ctx.get_web();
                this.contentTypeColl = web.get_contentTypes();
                var newcType = new SP.ContentTypeCreationInformation();
                newcType.set_name('My Content Type');
                newcType.set_description('My list content type');
                newcType.set_group("List Content Types");

                ctx.executeQueryAsync(function () {
                    createSfield(web, ctx);
                    var field = ctx.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("EmployeeName");
                    var cType = ctx.get_site().get_rootWeb().get_contentTypes().getById("0x0100A33D9AD9805788419BDAAC2CCB37509E");
                    var fInfo = new SP.FieldLinkCreationInformation();
                    var fLink = cType.get_fieldLinks().add(fInfo);
                    ctx.executeQueryAsync(function () {
                        alert("Content Type and field created successfully");
                    }, function (sender, args) {
                        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                }, function (sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        function createSfield(web, ctx) {
            var fields = web.get_fields();
            var fieldSchema = '<Field Type="Text" \Name = "EmployeeName" \DisplayName = "Employee Name" \Required = "TRUE" \Group = "Employee Data" /> ';    
            ctx.executeQueryAsync(Success, Fail);
        function Success() {
            alert('SiteColumn is created successfully');
        function Fail() {
            alert('site column Creation Failed');
    <input id="btnCreateContentType" onclick="createCType()" type="button" value="Create Content Type and field" />

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 (‘My Content Type’), we can check the field of content type. Please refer the below image for better reference.

Content type created in the below image.

Field added to the content type in the below image.

• Create Content type and its field using JSOM.
• How to create content type and its field using JSOM.
• How to create list content type under site content type.

Leave a Reply

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