In Focus

Add And Remove Users From Security Group In SharePoint 2016, Using REST API

In this article, you will learn how to add and remove users from Security Group in SharePoint 2016, using REST API.

SharePoint 2016 general availability has been announced in the Future Of SharePoint conference in May 2016. The series that discusses the installation of SharePoint 2016 in Azure can be found at C# Corner at the below links:

In this article, we will see how to add and remove SharePoint Users from Security Groups in SharePoint 2016, using REST API. As an initial prerequisite, ensure that the user running the below scripts must have Site Collection administrator privileges.
We will use REST to connect to the SharePoint for performing the user operations. The Scope of the article involves the following operations, using REST:

  • Add user to a SharePoint Group
  • Remove existing user from the Group

Add Users to SharePoint Group

We can add users to the SharePoint Group by issuing a POST AJAX request. The REST URL endpoint used for the operation, is:

/_api/web/sitegroups/getbyname('Employee')/users


We will be creating a key value pair of the information which will be used to add the user to the SharePoint Group. The user addition information property is as shown below. It will be sent as JSON in the ‘data’ attribute of the AJAX REST call.

  1. var metadata = {  
  2.     __metadata: {  
  3.         'type''SP.User'  
  4.     },  
  5.     LoginName: 'i:0#.w|SharePointHOL\\Priyaranjan'  
  6. };  
Within the _metadata attribute, we have to specify the value for ‘type’ which will specify what object is being created. In our case, it is user. Hence, we will be specifying ‘SP.User’. Since we are using Claims authentication in SharePoint 2016, we have to ensure that the login name is in the format “i:0#.w|Domain\\UserName”. The header section will look like the following:
  1. headers: {  
  2.     "accept""application/json;odata=verbose",  
  3.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  4.     "content-Type""application/json;odata=verbose"  
  5. },  
Here, Accept attribute specifies the data type for the return value and content-type defines the data type for the data sent to the server. In POST request, we have to send the X-RequestDigest value along with the request for form validation without which we'll get a validation error. To fulfil this, we will be assigning the $("#__REQUESTDIGEST").val() which sets the value of the form digest control, present within the page to X-RequestDigest key.

Output

The user has successfully been added to the SharePoint Group and the notification has come up in the console.

Output

Going to the Group page, we can see the newly added user.

Output

Full Code

The full code for user addition to the Group is, as shown below:
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newUserUrl = "/_api/web/sitegroups/getbyname('Employee')/users";  
  5.         var metadata = {  
  6.             __metadata: {  
  7.                 'type''SP.User'  
  8.             },  
  9.             LoginName: 'i:0#.w|SharePointHOL\\Priyaranjan'  
  10.         };  
  11.         addNewUser(newUserUrl, metadata)  
  12.     });  
  13.   
  14.     function addNewUser(newUserUrl, metadata) {  
  15.         $.ajax({  
  16.             url: _spPageContextInfo.webAbsoluteUrl + newUserUrl,  
  17.             type: "POST",  
  18.             headers: {  
  19.                 "accept""application/json;odata=verbose",  
  20.                 "X-RequestDigest": ("#__REQUESTDIGEST").val(),  
  21.                 "content-Type": application / json;odata = verbose "  
  22.             },  
  23.             data: JSON.stringify(metadata),  
  24.             success: function(data) {  
  25.                 console.log(data);  
  26.                 console.log("New User has been successfully to the SharePoint Group.");  
  27.             },  
  28.             error: function(error) {  
  29.                 alert(JSON.stringify(error));  
  30.             }  
  31.         });  
  32.     }  
  33. </script>  
Remove the User from Group

We can remove the user from the SharePoint group by issuing a POST AJAX request. The REST URL endpoint, used for the operation, is

/_api/web/sitegroups/getbyname('Employee')/users/removeByLoginName


We will be creating a key value pair of the information which will be used to delete the user from the SharePoint Group. The user deletion information property is, as shown below. It will be sent as JSON in the ‘data’ attribute of the AJAX REST call.
  1.  var metadata = {  
  2. 'loginName''i:0#.w|SharePointHOL\\Priyaranjan'  
  3. };  
The header section will look like the following.
  1. headers: {  
  2.     "accept""application/json;odata=verbose",  
  3.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  4.     "content-Type""application/json;odata=verbose"  
  5. },  
Here, accept attribute specifies the data type for the return value and content-type defines the data type for the data sent to the server. In POST request, we have to send the X-RequestDigest value along with the request for the form validation without which we will get validation error. To fulfil this, we will be assigning the $("#__REQUESTDIGEST").val() which sets the value of the form digest control present within the page to X-RequestDigest key.

Output

After running the script, the user will be removed from the SharePoint Group and we will get the notification in the console.

Output

Full Code

The full code for the user removal operation is, as shown below:
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var removeUserUrl = "/_api/web/sitegroups/getbyname('Employee')/users/removeByLoginName";  
  5.         var metadata = {  
  6.             'loginName''i:0#.w|SharePointHOL\\Priyaranjan'  
  7.         };  
  8.         removeUser(removeUserUrl, metadata)  
  9.     });  
  10.   
  11.     function removeUser(removeUserUrl, metadata) {  
  12.         $.ajax({  
  13.             url: _spPageContextInfo.webAbsoluteUrl + removeUserUrl,  
  14.             type: "POST",  
  15.             headers: {  
  16.                 "accept""application/json;odata=verbose",  
  17.                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  18.                 "content-Type""application/json;odata=verbose"  
  19.             },  
  20.             data: JSON.stringify(metadata),  
  21.             success: function(data) {  
  22.                 console.log(data);  
  23.                 console.log("User has been successfully removed from the SharePoint Group.");  
  24.             },  
  25.             error: function(error) {  
  26.                 alert(JSON.stringify(error));  
  27.             }  
  28.         });  
  29.     }  
  30. </script>  
Let’s see how to implement the above scripts in SharePoint. Below steps will demonstrate how to work with the script for removing the user from the Group. Similarly, other scripts can also be tested. Save the script for user removal as a text file, and upload it to Site Assets Library.

SharePoint Implementation 
  • Go to the edit settings of the SharePoint page and click on Web part from the Insert tab.

    SharePoint Implementation

  • Add Content Editor Web part.

    Web part

  • Click on Edit Web part from Content Edit Web part. Assign the URL of the script text file and click on Apply.

    Edit Web art

  • Click on Apply and we can see the successful user removal message from the console.

Going to the Group where the user was added earlier, we can see that the Group is now empty. That indicates the removal of the user.

sites

Summary

Thus, we saw how to add a user to a SharePoint Group and remove existing users from the security group in SharePoint 2016, by using the REST API.