Manage Allow Multiple Selection in Task List Assigned to Field using JSOM

Implementation:

  • Declare the public variables.
  • Get Host Web URL and App web URL in Document Ready.
  • Then call MakeMultiSelectionToUserField method.
  • Get app context site from AddEnterpriseColumnToList method.
  • Get web and Task list.
  • Get assigned to filed by Internal name.
  • Then case to the assigned to field to user field.
  • Set Allow multiple values to True.
  • Load the user Field.
  • 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.   
  4. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  5. $(document).ready(function () {          
  6.      hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  7.      appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  8.      MakeMultiSelectionToUserField();  
  9. });  
  10. //Make assigned to field accept multivalues  
  11. function MakeMultiSelectionToUserField() {  
  12.     hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  13.     appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'))  
  14.     var context = new SP.ClientContext(appWebURL);  
  15.     var appCtxSite = new SP.AppContextSite(context, hostWebURL);  
  16.     //get Web  
  17.     var web = appCtxSite.get_web();  
  18.     //Get list by title  
  19.     var list = web.get_lists().getByTitle('Tasks');     
  20.     //Get assigned to  by internal name  
  21.     var field = list.get_fields().getByInternalNameOrTitle("AssignedTo");     
  22.     var userField = context.castTo(field, SP.FieldUser);  
  23.    //Manage allow multiple values for assigned to column  
  24.     userField.set_allowMultipleValues(true);  
  25.     userField.update();    
  26.     context.load(userField);  
  27.     context.executeQueryAsync(function () {  
  28.     //Success Method  
  29.         alert(userField.get_allowMultipleValues());  
  30.         console.log("Field added successfully!!" + field);  
  31.     }  
  32.        , function (sender, args) {  
  33. //Failure method Method  
  34.            console.log("Request failed" + args.get_message());  
  35.        });  
  36.   
  37. }  
  38.   
  39. //Getting host web url and app web url from query string using this method  
  40. function manageQueryStringParameter(paramToRetrieve) {  
  41.     var params = document.URL.split("?")[1].split("&");  
  42.     var strParams = "";  
  43.     for (var i = 0; i < params.length; i = i + 1) {  
  44.         var singleParam = params[i].split("=");  
  45.         if (singleParam[0] == paramToRetrieve) {  
  46.             return singleParam[1];  
  47.         }  
  48.     }  
  49. }  
Summary

In this blog, we have explored how to allow multiple selection users in SharePoint task list assigned to field using JavaScript Object model. I hope above explained solution is very useful to you. Happy Coding!