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. // 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. hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  6. appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
  7. MakeMultiSelectionToUserField();
  8. });
  9. //Make assigned to field accept multivalues
  10. function MakeMultiSelectionToUserField() {
  11. hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
  12. appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'))
  13. var context = new SP.ClientContext(appWebURL);
  14. var appCtxSite = new SP.AppContextSite(context, hostWebURL);
  15. //get Web
  16. var web = appCtxSite.get_web();
  17. //Get list by title
  18. var list = web.get_lists().getByTitle('Tasks');
  19. //Get assigned to by internal name
  20. var field = list.get_fields().getByInternalNameOrTitle("AssignedTo");
  21. var userField = context.castTo(field, SP.FieldUser);
  22. //Manage allow multiple values for assigned to column
  23. userField.set_allowMultipleValues(true);
  24. userField.update();
  25. context.load(userField);
  26. context.executeQueryAsync(function () {
  27. //Success Method
  28. alert(userField.get_allowMultipleValues());
  29. console.log("Field added successfully!!" + field);
  30. }
  31. , function (sender, args) {
  32. //Failure method Method
  33. console.log("Request failed" + args.get_message());
  34. });
  35. }
  36. //Getting host web url and app web url from query string using this method
  37. function manageQueryStringParameter(paramToRetrieve) {
  38. var params = document.URL.split("?")[1].split("&");
  39. var strParams = "";
  40. for (var i = 0; i < params.length; i = i + 1) {
  41. var singleParam = params[i].split("=");
  42. if (singleParam[0] == paramToRetrieve) {
  43. return singleParam[1];
  44. }
  45. }
  46. }
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!