Create Content Type using JavaScript object model in SharePoint Online Office 365

Create Content Type using JavaScript object model in SharePoint Online Office 365

In this post, we will discuss how we can create a content type using JavaScript object model in SharePoint Online Office 365. You can check out my article on Content types in SharePoint.

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

Also you can read:
Add use custom action to list ECB menu in SharePoint Online using JavaScript object model

How to get content database size using PowerShell in SharePoint 2016 and using SharePoint server object model

Configure workflow manager for SharePoint 2016 workflow in windows server 2012 r2

<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 object model in SharePoint Online Office 365
Create Content Type using JavaScript object model in SharePoint Online Office 365

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About 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

View all posts by Bijay Kumar →