Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Introduction
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 2016.
Code Example
For this particular example I have added a script editor web part inside a web part page and in that script editor web part I have added the below code.
Below is the full code:
<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.
Output
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:
Hope this will be helpful.