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.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>