Update App.js and add following code:
'use strict';
var context = SP.ClientContext.get_current();
var hostWebUrl, hostWebContext;
// This code runs when the DOM is ready and creates a context object which is
// needed to use the SharePoint object model
$(document).ready(function () {
hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
hostWebContext = new SP.AppContextSite(context, hostWebUrl);
});
// Retrieve a query string value
function getQueryStringParameter(paramToRetrieve) {
var params = document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve) return singleParam[1];
}
}
//Add Site Columns to Content Type
var hostWebContentTypes;
var contentTypeName = 'Employee'; // Name of the Content Type where Colums will be added
var columnsInternalName = ["EmployeeId", "EmployeeName"]; //Internal Names of the Site Columns
var createdColumns = new Array();
function addColumnsToContentType() {
var hostWeb = hostWebContext.get_web();
//Get the columns that need to be added to Content type and store the objects in an Array.
for (var iCreatedFieldsCounter = 0; iCreatedFieldsCounter < columnsInternalName.length; iCreatedFieldsCounter++) {
createdColumns[iCreatedFieldsCounter] = hostWeb.get_fields().getByInternalNameOrTitle(columnsInternalName[iCreatedFieldsCounter]);
context.load(createdColumns[iCreatedFieldsCounter]);
}
//Get Host Web Content type Collection
hostWebContentTypes = hostWeb.get_contentTypes();
context.load(hostWebContentTypes);
context.executeQueryAsync(
function () {
//Call function to add columns to Content Type
addColumns(contentTypeName, columnsInternalName, createdColumns);
},
function onItemsRefetchedFail(sender, args) {
alert('Failed to fetch columns and Content Type. Error:' + args.get_message() + '\n' + args.get_stackTrace());
});
}
// Add columns
function addColumns(ctypeName, fieldsInternalName, createdFields) {
//Find the Content Type and then add Fields to it
var createdContentType;
var contentTypeEnumerator = hostWebContentTypes.getEnumerator();
while (contentTypeEnumerator.moveNext()) {
var contentType = contentTypeEnumerator.get_current();
if (contentType.get_name() === ctypeName) {
createdContentType = contentType;
var fieldRef = new Array();
for (var iAddFieldsCounter = 0; iAddFieldsCounter < createdFields.length; iAddFieldsCounter++) {
fieldRef[iAddFieldsCounter] = new SP.FieldLinkCreationInformation();
fieldRef[iAddFieldsCounter].set_field(createdFields[iAddFieldsCounter]);
createdContentType.get_fieldLinks().add(fieldRef[iAddFieldsCounter]);
createdContentType.update(true);
}
context.load(createdContentType);
context.executeQueryAsync(onAddFieldToContentTypeSuccess, onAddFieldToContentTypeFail);
}
}
}
function onAddFieldToContentTypeSuccess() {
alert('Site Columns added to Content Type.');
}
function onAddFieldToContentTypeFail(sender, args) {
alert('Failed to add Site Columns to Content Type. Error:' + args.get_message() + '\n' + args.get_stackTrace());
}
//End