Add Enterprise Keyword Column In SharePoint List Using JSOM

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. 
  1. //Varibles Declaration  
  2. var appWebURL, hostWebURL, appCtxSite, context;  
  3. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  4. $(document).ready(function ()  
  5. {  
  6.     hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  7.     appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  8.     AddEnterpriseColumnsToList();  
  9. });  
  10. //Add column to list  
  11. function AddEnterpriseColumnsToList()  
  12. {  
  13.     //get app content site using hostweb URL  
  14.     var appCtxSite = new SP.AppContextSite(context, hostWebURL);  
  15.     //Get Web using appcontext site  
  16.     var web = appCtxSite.get_web();  
  17.     //get task list from web  
  18.     var list = web.get_lists().getByTitle('Tasks');  
  19.     //Get Enterprise column from site  
  20.     var field = context.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle("TaxKeyword");  
  21.     //Add Enterprise field to List  
  22.     list.get_fields().add(field);  
  23.     context.load(list);  
  24.     context.executeQueryAsync(function ()  
  25.     {  
  26.         //Success method  
  27.         console.log("Field added successfully!!")  
  28.     }, function (sender, args)  
  29.     {  
  30.         //Error method  
  31.         console.log("List Items failed" + args.get_message());  
  32.     });  
  33. }  
  34. //Get value from Query string  
  35. function manageQueryStringParameter(paramToRetrieve)  
  36. {  
  37.     var params = document.URL.split("?")[1].split("&");  
  38.     var strParams = "";  
  39.     for (var i = 0; i < params.length; i = i + 1)  
  40.     {  
  41.         var singleParam = params[i].split("=");  
  42.         if (singleParam[0] == paramToRetrieve)  
  43.         {  
  44.             return singleParam[1];  
  45.         }  
  46.     }  
  47. }  
Note: Make sure to refer the taxonomy.js file in aspx page.

taxonomy

AppManifiest.xml
file add the permission level to access the list in your host web.

add the permission

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.