Site Content Type in SharePoint 2013 using JSOM

      Modify App.js file and replace its content by 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. }