Implementation
In my previous two articles you saw, how to:
- Create Site Columns On Host Web From App Web Using JavaScript In SharePoint 2013.
- Create Site Content Type In SharePoint 2013 using JSOM.
So, if you open the created Content type, you only see ‘Name’ and ‘Title’ fields there, which are basically added from Parent Content Types, now we will see how to add Site Columns to this Site Content Type present on Host Web from App Web.
We will perform following operations to achieve this:
- Get Host Web Context using App Web.
- Fetch already created Site Columns.
- Get the required Content Type from Host Web Content Type collection.
- Add Site Columns to this Content Type and update the Content Type.
Get Started:
- Add a button on page to ‘Add Columns to Content Type’.
- <%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
- <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
- <div>
- <input type="button" id="btnAddColumns" value="Add Columns to Content Type" onclick="addColumnsToContentType()"/>
- </div>
- </asp:Content>
- Update App.js and add the 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());
- }
-
- Build and Deploy the solution and click ‘Add Columns to Content Type’ button. Site Columns will be added to Content Type.
- Navigate to Host Web Site Content Type and you can see the newly added columns there.
Conclusion
Using this JSOM approach, you can add Site Columns to Content type on Host Web from App Web. You can use this approach to add columns to App web Content Type too.