Update App.js and add following code:
- 'use strict';
-
- var context = SP.ClientContext.get_current();
-
- var hostWebUrl, hostWebContext;
-
-
-
-
-
- $(document).ready(function () {
-
- hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
-
- hostWebContext = new SP.AppContextSite(context, hostWebUrl);
-
- });
-
-
-
-
-
- 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];
-
- }
-
- }
-
-
-
- var hostWebContentTypes;
-
- var contentTypeName = 'Employee';
-
- var columnsInternalName = ["EmployeeId", "EmployeeName"];
-
- var createdColumns = new Array();
-
- function addColumnsToContentType() {
-
- var hostWeb = hostWebContext.get_web();
-
-
-
- for (var iCreatedFieldsCounter = 0; iCreatedFieldsCounter < columnsInternalName.length; iCreatedFieldsCounter++) {
-
- createdColumns[iCreatedFieldsCounter] = hostWeb.get_fields().getByInternalNameOrTitle(columnsInternalName[iCreatedFieldsCounter]);
-
- context.load(createdColumns[iCreatedFieldsCounter]);
-
- }
-
-
-
- hostWebContentTypes = hostWeb.get_contentTypes();
-
- context.load(hostWebContentTypes);
-
- context.executeQueryAsync(
-
- function () {
-
-
-
- addColumns(contentTypeName, columnsInternalName, createdColumns);
-
- },
-
- function onItemsRefetchedFail(sender, args) {
-
- alert('Failed to fetch columns and Content Type. Error:' + args.get_message() + '\n' + args.get_stackTrace());
-
- });
-
- }
-
-
-
- function addColumns(ctypeName, fieldsInternalName, createdFields) {
-
-
-
- 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());
-
- }
-
-