Add Site Columns to Site Content Type on Host Web using JSOM

Update App.js and add following code: 
  1. 'use strict';  
  2.    
  3. var context = SP.ClientContext.get_current();  
  4.    
  5. var hostWebUrl, hostWebContext;  
  6.    
  7. // This code runs when the DOM is ready and creates a context object which is   
  8.   
  9. // needed to use the SharePoint object model  
  10.    
  11. $(document).ready(function () {  
  12.    
  13.     hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  14.    
  15.     hostWebContext = new SP.AppContextSite(context, hostWebUrl);  
  16.    
  17. });  
  18.    
  19.    
  20.    
  21. // Retrieve a query string value  
  22.    
  23. function getQueryStringParameter(paramToRetrieve) {  
  24.    
  25.     var params = document.URL.split("?")[1].split("&");  
  26.    
  27.     for (var i = 0; i < params.length; i = i + 1) {  
  28.    
  29.         var singleParam = params[i].split("=");  
  30.    
  31.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  32.    
  33.     }  
  34.    
  35. }  
  36.    
  37. //Add Site Columns to Content Type  
  38.    
  39. var hostWebContentTypes;  
  40.    
  41. var contentTypeName = 'Employee'// Name of the Content Type where Colums will be added  
  42.    
  43. var columnsInternalName = ["EmployeeId""EmployeeName"]; //Internal Names of the Site Columns  
  44.    
  45. var createdColumns = new Array();  
  46.    
  47. function addColumnsToContentType() {  
  48.    
  49.     var hostWeb = hostWebContext.get_web();  
  50.    
  51.     //Get the columns that need to be added to Content type and store the objects in an Array.  
  52.    
  53.     for (var iCreatedFieldsCounter = 0; iCreatedFieldsCounter < columnsInternalName.length; iCreatedFieldsCounter++) {  
  54.    
  55.         createdColumns[iCreatedFieldsCounter] = hostWeb.get_fields().getByInternalNameOrTitle(columnsInternalName[iCreatedFieldsCounter]);  
  56.    
  57.         context.load(createdColumns[iCreatedFieldsCounter]);  
  58.    
  59.     }  
  60.    
  61.     //Get Host Web Content type Collection  
  62.    
  63.     hostWebContentTypes = hostWeb.get_contentTypes();  
  64.    
  65.     context.load(hostWebContentTypes);  
  66.    
  67.     context.executeQueryAsync(  
  68.    
  69.   function () {  
  70.    
  71.       //Call function to add columns to Content Type  
  72.    
  73.       addColumns(contentTypeName, columnsInternalName, createdColumns);  
  74.    
  75.   },  
  76.    
  77.   function onItemsRefetchedFail(sender, args) {  
  78.    
  79.       alert('Failed to fetch columns and Content Type. Error:' + args.get_message() + '\n' + args.get_stackTrace());  
  80.    
  81.   });  
  82.    
  83. }  
  84.    
  85. // Add columns  
  86.    
  87. function addColumns(ctypeName, fieldsInternalName, createdFields) {  
  88.    
  89.     //Find the Content Type and then add Fields to it  
  90.    
  91.     var createdContentType;  
  92.    
  93.     var contentTypeEnumerator = hostWebContentTypes.getEnumerator();  
  94.    
  95.     while (contentTypeEnumerator.moveNext()) {  
  96.    
  97.         var contentType = contentTypeEnumerator.get_current();  
  98.    
  99.         if (contentType.get_name() === ctypeName) {  
  100.    
  101.             createdContentType = contentType;  
  102.    
  103.             var fieldRef = new Array();  
  104.    
  105.             for (var iAddFieldsCounter = 0; iAddFieldsCounter < createdFields.length; iAddFieldsCounter++) {  
  106.    
  107.                 fieldRef[iAddFieldsCounter] = new SP.FieldLinkCreationInformation();  
  108.    
  109.                 fieldRef[iAddFieldsCounter].set_field(createdFields[iAddFieldsCounter]);  
  110.    
  111.                 createdContentType.get_fieldLinks().add(fieldRef[iAddFieldsCounter]);  
  112.    
  113.                 createdContentType.update(true);  
  114.    
  115.             }  
  116.    
  117.             context.load(createdContentType);  
  118.    
  119.             context.executeQueryAsync(onAddFieldToContentTypeSuccess, onAddFieldToContentTypeFail);  
  120.    
  121.         }  
  122.    
  123.     }  
  124.    
  125. }  
  126.    
  127. function onAddFieldToContentTypeSuccess() {  
  128.    
  129.     alert('Site Columns added to Content Type.');  
  130.    
  131. }  
  132.    
  133. function onAddFieldToContentTypeFail(sender, args) {  
  134.    
  135.     alert('Failed to add Site Columns to Content Type. Error:' + args.get_message() + '\n' + args.get_stackTrace());  
  136.    
  137. }  
  138.    
  139. //End