Create Content Type using JavaScript object model (jsom) in SharePoint Online/2013/2016

This jsom SharePoint tutorial explains, how to create a content type using the JavaScript object model (jsom) in SharePoint Online Office 365 or SharePoint 2013/2016.

If you are new to content type then check out an article on Content type in SharePoint 2016/2013/Online tutorial.

Create Content Type using JSOM in SharePoint

For this particular example I have added a script editor web part and in the script editor web part in a SharePoint web part page.

Here, I have taken a button and on button click, it will create the content type in SharePoint site.

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
var contentTypeCollection;

function createContentType() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var web = clientContext.get_web();
this.contentTypeCollection = web.get_contentTypes();
this.contentType = contentTypeCollection.getById("0x0101");
var newContentType = new SP.ContentTypeCreationInformation();
newContentType.set_name('My Custom Content Type');
newContentType.set_description('My custom content type');
newContentType.set_parentContentType(contentType);
this.contentTypeCollection.add(newContentType);
clientContext.load(this.contentTypeCollection);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}

function onQuerySucceeded() {
alert("Content Type created successfully")
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

<input id="btnCreateContentType" onclick="createContentType()" type="button" value="Create Content Type" />

Once you clicked on the button the content type will get created. You can see the content type from the site settings page. Go to site settings -> then click on “Site content types” which is under Web Designer Galleries. Then you can see your content type like below:

create content type using JavaScript sharepoint

You may like following SharePoint content type tutorials:

This is how we can create a content type using jsom in SharePoint 2013/2016 or SharePoint Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

>