Create Site Content Type In SharePoint 2013 using JSOM

Implementation

  1. Get Host Web Context
  2. Get Site Content Types Collection
  3. Define new Content Type
  4. Add the new Content Type to collection

Get Started:

  1. Create a SharePoint –hosted ‘Apps for SharePoint 2013’ project using Visual Studio 2012/2013.

  2. Provide required permission to App for creating Content Type in AppManifest.xml.

  3. Add a button on page to create Content Type.
    1. <%-- The markup and script in the following Content element will be placed in the <body> of the page --%>  
    2. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">  
    3.    <div>  
    4.       <input type="button" id="btnCreateContent Type" value="Create Content Type" onclick="createContentType()" />  
    5.    </div>  
    6. </asp:Content>  
  4. Modify App.js file and replace its content by the following code.
    1. 'use strict';  
    2.   
    3. var context = SP.ClientContext.get_current(); //App Web Context  
    4. var hostWebUrl, hostWebContext;  
    5.   
    6. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
    7. $(document).ready(function () {  
    8.     //Get the URI decoded URLs.  
    9.     hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));      
    10.     hostWebContext = new SP.AppContextSite(context, hostWebUrl);  
    11. });  
    12.   
    13. // Retrieve a query string value.  
    14. function getQueryStringParameter(paramToRetrieve) {  
    15.     var params = document.URL.split("?")[1].split("&");  
    16.     for (var i = 0; i < params.length; i = i + 1) {  
    17.         var singleParam = params[i].split("=");  
    18.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
    19.     }  
    20. }  
    21.   
    22. //Create Content Type  
    23. function createContentType() {  
    24.     if (hostWebContext != undefined && hostWebContext != null) {  
    25.         var hostWeb = hostWebContext.get_web();  
    26.         var contentTypeCollection = hostWeb.get_contentTypes();  
    27.         //Get Document Content type by its Id. We will use Document Content Type as Base Content Type  
    28.         //to create a new Content Type.  
    29.         //Refer article for Ids of other Content Types.  
    30.         var contentType = contentTypeCollection.getById("0x0101");  
    31.         //Creating new Content Type  
    32.         var newContentType = new SP.ContentTypeCreationInformation();  
    33.         newContentType.set_name('Employee');  
    34.         newContentType.set_group('Employee Details');  
    35.         newContentType.set_description('Content Type for Employee Details.');  
    36.         //Set Base Content Type  
    37.         newContentType.set_parentContentType(contentType);  
    38.         contentTypeCollection.add(newContentType);  
    39.         context.load(contentTypeCollection);  
    40.         context.executeQueryAsync(  
    41.             function () {  
    42.                 alert('Content type created successfully on Host Web.');  
    43.             },  
    44.            function onContenttypeFailed(sender, args) {  
    45.                alert('Content type creation failed. Error: ' + args.get_message() + '\n' + args.get_stackTrace());  
    46.            });  
    47.     }  
    48. }  
  5. Build and Deploy the solution.

  6. Click on ‘Create Content Type’ button, Content Type will be created on Host Web.

    Create Content Type

  7. Navigate to Host Web Site Content types, you will see ‘Employee’ Content Type under ‘Employee Details’ group.

    Employee Details

Additional Data

The following table shows Content Type Ids, which can be used as Base Content Type.

Table

Hierarchy:

Hierarchy

Conclusion

Using this JSOM, you can create Content Type on Host Web. By using only App Web context, you can create Content type on App Web. To use any other Content type as Base Content type, you can refer to the above table. If you do not provide any base Content type, by default it is set to ‘Item’ Content type.