Manage Allow Multiple Selection in Task List Assigned to Field using JSOM
In this blog, I would like to share the way to set the allow multiple values in SharePoint task list assigned to field using SharePoint hosted app.
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.
-
- var appWebURL, hostWebURL, appCtxSite, context;
-
-
- $(document).ready(function () {
- hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
- MakeMultiSelectionToUserField();
- });
-
- function MakeMultiSelectionToUserField() {
- hostWebURL = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebURL = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'))
- var context = new SP.ClientContext(appWebURL);
- var appCtxSite = new SP.AppContextSite(context, hostWebURL);
-
- var web = appCtxSite.get_web();
-
- var list = web.get_lists().getByTitle('Tasks');
-
- var field = list.get_fields().getByInternalNameOrTitle("AssignedTo");
- var userField = context.castTo(field, SP.FieldUser);
-
- userField.set_allowMultipleValues(true);
- userField.update();
- context.load(userField);
- context.executeQueryAsync(function () {
-
- alert(userField.get_allowMultipleValues());
- console.log("Field added successfully!!" + field);
- }
- , function (sender, args) {
-
- console.log("Request 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];
- }
- }
- }
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!