Creating Content Type and Field Using JSOM
Softree Technology > Blogs > SharePoint > Creating Content Type and Field Using JSOM

Creating Content Type and Field Using JSOM

  • Posted by: Softree Technology

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()”.

<html>
<body>
    <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_id("0x0100A33D9AD9805788419BDAAC2CCB37509E");
                newcType.set_group("List Content Types");

                this.contentTypeColl.add(newcType);
                ctx.load(this.contentTypeColl);
                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");
                    ctx.load(cType);
                    ctx.load(field);
                    var fInfo = new SP.FieldLinkCreationInformation();
                    fInfo.set_field(field);
                    var fLink = cType.get_fieldLinks().add(fInfo);
                    cType.update(true);
                    ctx.load(fLink);
                    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" /> ';    
            fields.addFieldAsXml(fieldSchema);
            ctx.executeQueryAsync(Success, Fail);
        }
        function Success() {
            alert('SiteColumn is created successfully');
        }
        function Fail() {
            alert('site column Creation Failed');
        } 
    </script>
    <input id="btnCreateContentType" onclick="createCType()" type="button" value="Create Content Type and field" />
</body>
</html>

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.

Keywords:
• 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.

Author: Softree Technology

Leave a Reply