My scenario is, I need to create Enterprise keyword column in SharePoint host web list using SharePoint hosted app.
After google search, I found that. We have more articles to add the enterprise keyword column using SP object mode but using JavaScript I couldn’t find. I hope the following explained solution is very useful to you,
Implementation
- Get Host Web URL and App web URL in Document Ready.
- Then call AddEnterpriseColumnToList method.
- Get app context site from AddEnterpriseColumnToList method.
- Get web and Task list.
- Add Enterprise keyword column to List.
- Load the List.
- Execute the request.
- Show result in success method.
- Failure method are used to catch the errors.
-
- var appWebURL, hostWebURL, appCtxSite, context;
-
- $(document).ready(function ()
- {
- hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
- AddEnterpriseColumnsToList();
- });
-
- function AddEnterpriseColumnsToList()
- {
-
- var appCtxSite = new SP.AppContextSite(context, hostWebURL);
-
- var web = appCtxSite.get_web();
-
- var list = web.get_lists().getByTitle('Tasks');
-
- var field = context.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("TaxKeyword");
-
- list.get_fields().add(field);
- context.load(list);
- context.executeQueryAsync(function ()
- {
-
- console.log("Field added successfully!!")
- }, function (sender, args)
- {
-
- console.log("List Items failed" + args.get_message());
- });
- }
-
- function manageQueryStringParameter(paramToRetrieve)
- {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1)
- {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- {
- return singleParam[1];
- }
- }
- }
Note: Make sure to refer the taxonomy.js file in aspx page.
AppManifiest.xml file add the permission level to access the list in your host web.
References
This article says how to
activate the Enterprise keyword column in list using OOB feature.
Summary
In this article we have explored how to add the enterprise keyword column in list using JSOM with SharePoint hosted app.