Introduction

This article will help beginner and developers who want to work with AngularJS and jQuery for People Picker columns.

Objective

Post a "Person" and "Group" column in SharePoint list with autocomplete input of HTML using Angular.js and jQuery with REST call.

Step 1

Create a SharePoint list with a single person and group column as shown below. Keep ‘Allow multiple selections:’ as ‘No’.

SharePoint

Then, the created list looks as below.

SharePoint
Step2

Create a page in SharePoint and include a content editor with the following HTML.

HTML code

  1. <body ng-app="myApp" ng-controller="MainCtrl">
  2. <label for="tags">Assigned To: </label>
  3. <input id="Persons" ng-model="SelectedPerson"/><br/><br/>
  4. <input type="button" ng-click="AddToList()" value="Submit"/>
  5. </body>

Result

SharePoint

Step 3

Build a script for autocomplete input.

We have mainly 3 steps to build the script as follows.

Step 4

Include the following jQuery and Angular.js references in the page header.

References

  1. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  2. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  3. <script src="https://code.angularjs.org/1.3.0/angular.js"></script>

Finally, the overall code looks like below.

CODE

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>AutocompleteByAJS</title>
  5. <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"/>
  6. <link rel="stylesheet" href="https://jqueryui.com/resources/demos/style.css"/>
  7. <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  8. <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  9. <script src="https://code.angularjs.org/1.3.0/angular.js"></script>
  10. </head>
  11. <body ng-app="myApp" ng-controller="MainCtrl">
  12. <label for="tags">Assigned To: </label>
  13. <input id="Persons" ng-model="SelectedPerson"/><br/><br/>
  14. <input type="button" ng-click="AddToList()" value="Submit"/>
  15. </body>
  16. <script type="text/javascript">
  17. var app = angular.module('myApp', []);
  18. var peoplePickerUsers =[];
  19. var AllpeoplePickerUsers =[];
  20. var CurPersonId
  21. var listName="SampleListforAutocomplete";
  22. app.controller('MainCtrl',function($scope,$http) {
  23. $.ajax
  24. ({
  25. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers",
  26. type: "GET",
  27. headers:
  28. {
  29. "Accept": "application/json;odata=verbose"
  30. },
  31. success: function(data)
  32. {
  33. AllpeoplePickerUsers = data.d.results;
  34. console.log(AllpeoplePickerUsers);
  35. for (var i = 0; i < AllpeoplePickerUsers.length; i++) {
  36. var property = AllpeoplePickerUsers[i].Title;
  37. peoplePickerUsers.push(property);
  38. }
  39. },
  40. error: function(error)
  41. {
  42. alert(JSON.stringify(error));
  43. }
  44. });
  45. $( function() {
  46. $( "#Persons" ).autocomplete({
  47. source: peoplePickerUsers
  48. });
  49. } );
  50. $scope.AddToList=function(){
  51. for(var i=0;i<AllpeoplePickerUsers.length;i++){
  52. if ($scope.SelectedPerson=== AllpeoplePickerUsers[i].Title)
  53. {
  54. CurPersonId=AllpeoplePickerUsers[i].Id;
  55. }
  56. }
  57. var datavalue = "{__metadata:{'type':'SP.Data."+listName+"ListItem'},AssigningToId:'"+CurPersonId+"'}";
  58. // alert(datavalue);
  59. $http({
  60. url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('"+listName+"')/items",
  61. method: "POST",
  62. headers:
  63. {
  64. "Accept": "application/json;odata=verbose",
  65. "Content-Type": "application/json;odata=verbose",
  66. "X-RequestDigest": $("#__REQUESTDIGEST").val(),
  67. "X-HTTP-Method": "POST"
  68. },
  69. data: datavalue
  70. })
  71. .then(function(response) {
  72. alert("success");
  73. },
  74. function(response) {
  75. alert("failed");
  76. });
  77. }
  78. });
  79. </script>
  80. </html>

Let’s see the results on screen,

Auto-complete

SharePoint
Pick the user from the input element and click Submit.

SharePoint

Go back to the SharePoint list.

SharePoint

Conclusion

This will be useful when someone wants to work on the People Picker column or to get the list of all the site's members.