{"id":6271,"date":"2020-09-03T12:55:24","date_gmt":"2020-09-03T12:55:24","guid":{"rendered":"https:\/\/www.softreetechnology.com\/?p=6271"},"modified":"2020-09-03T12:55:24","modified_gmt":"2020-09-03T12:55:24","slug":"creating-content-type-and-field-using-jsom","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/","title":{"rendered":"Creating Content Type and Field Using JSOM"},"content":{"rendered":"\n<p>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. <\/p>\n\n\n\n<p>Providing\nthe steps below to create content type and its fields.<\/p>\n\n\n\n<p><strong>Step 1 :<\/strong> First get the Current Context.<\/p>\n\n\n\n<p><strong>Step 2 : <\/strong>We have to get the content type collection and then have to create a new content type by using &#8220;SP.ContentTypeCreationInformation&#8221;.Here we must set the new content type name, description, ID, group.<\/p>\n\n\n\n<p><strong>Step 3 : <\/strong>Declared a function &#8220;createSfield()&#8221; for creating site field. Here I have created a text field having name &#8220;EmployeeName&#8221; and groupname &#8220;Employee Data&#8221; by using &#8220;addFieldAsXml&#8221;.And called this function inside the executequery() of fuction &#8220;createCType()&#8221;.<\/p>\n\n\n\n<p><strong>Step 4 : <\/strong>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 &#8220;SP.FieldLinkCreationInformation()&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html>\n&lt;body>\n    &lt;script type=\"text\/javascript\">\n        var contentTypeColl;\n        function createCType() {\n            var ctx = new SP.ClientContext.get_current();\n            if (ctx != undefined &amp;&amp; ctx != null) {\n                var web = ctx.get_web();\n                this.contentTypeColl = web.get_contentTypes();\n                var newcType = new SP.ContentTypeCreationInformation();\n                newcType.set_name('My Content Type');\n                newcType.set_description('My list content type');\n                newcType.set_id(\"0x0100A33D9AD9805788419BDAAC2CCB37509E\");\n                newcType.set_group(\"List Content Types\");\n\n                this.contentTypeColl.add(newcType);\n                ctx.load(this.contentTypeColl);\n                ctx.executeQueryAsync(function () {\n                    createSfield(web, ctx);\n                    var field = ctx.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle(\"EmployeeName\");\n                    var cType = ctx.get_site().get_rootWeb().get_contentTypes().getById(\"0x0100A33D9AD9805788419BDAAC2CCB37509E\");\n                    ctx.load(cType);\n                    ctx.load(field);\n                    var fInfo = new SP.FieldLinkCreationInformation();\n                    fInfo.set_field(field);\n                    var fLink = cType.get_fieldLinks().add(fInfo);\n                    cType.update(true);\n                    ctx.load(fLink);\n                    ctx.executeQueryAsync(function () {\n                        alert(\"Content Type and field created successfully\");\n                    }, function (sender, args) {\n                        alert('Request failed. ' + args.get_message() + '\\n' + args.get_stackTrace());\n                    });\n                }, function (sender, args) {\n                alert('Request failed. ' + args.get_message() + '\\n' + args.get_stackTrace());\n                });\n            }\n        }\n        function createSfield(web, ctx) {\n            var fields = web.get_fields();\n            var fieldSchema = '&lt;Field Type=\"Text\" \\Name = \"EmployeeName\" \\DisplayName = \"Employee Name\" \\Required = \"TRUE\" \\Group = \"Employee Data\" \/> ';    \n            fields.addFieldAsXml(fieldSchema);\n            ctx.executeQueryAsync(Success, Fail);\n        }\n        function Success() {\n            alert('SiteColumn is created successfully');\n        }\n        function Fail() {\n            alert('site column Creation Failed');\n        } \n    &lt;\/script>\n    &lt;input id=\"btnCreateContentType\" onclick=\"createCType()\" type=\"button\" value=\"Create Content Type and field\" \/>\n&lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<p>After running the code, we can check the content type in Site content\ntype option under site setting. Then clicking on the content type title (\u2018My\nContent Type\u2019), we can check the field of content type. Please refer the below\nimage for better reference.<\/p>\n\n\n\n<p>Content type created in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/a.png\" alt=\"\" class=\"wp-image-6272\" width=\"516\" height=\"243\"\/><\/figure>\n\n\n\n<p>Field added to the content type in the below image.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/b-1024x458.png\" alt=\"\" class=\"wp-image-6273\" width=\"704\" height=\"315\"\/><\/figure>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\"><em>Keywords<\/em><\/span><\/strong><em>:<\/em><br> \u2022 Create Content type and its field using JSOM.<br> \u2022 How to create content type and its field using JSOM.<br> \u2022 How to create list content type under site content type.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-6271","post","type-post","status-publish","format-standard","hentry","category-sharepoint"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Content Type and Field Using JSOM - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Content Type and Field Using JSOM - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-03T12:55:24+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating Content Type and Field Using JSOM - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/","og_locale":"en_US","og_type":"article","og_title":"Creating Content Type and Field Using JSOM - Softree Technology","og_description":"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 [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/","og_site_name":"Softree Technology","article_published_time":"2020-09-03T12:55:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Creating Content Type and Field Using JSOM","datePublished":"2020-09-03T12:55:24+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/"},"wordCount":259,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#primaryimage"},"thumbnailUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/a.png","articleSection":["SharePoint"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/","url":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/","name":"Creating Content Type and Field Using JSOM - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#primaryimage"},"thumbnailUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/a.png","datePublished":"2020-09-03T12:55:24+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#primaryimage","url":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/a.png","contentUrl":"https:\/\/www.softreetechnology.com\/wp-content\/uploads\/2020\/09\/a.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/sharepoint\/creating-content-type-and-field-using-jsom\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Creating Content Type and Field Using JSOM"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/6271","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=6271"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/6271\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=6271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=6271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=6271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}