SharePoint Rest API: Add user to SharePoint group and Retrieve all users from SharePoint group

This SharePoint rest api tutorial explains, how to add a user to SharePoint permission group using Rest API. Also, we will see how to get all users from a SharePoint 2013 permission group using REST API.

If you are new to rest api, then check out SharePoint rest api tutorial and examples.

Add User to SharePoint group using REST API

Below is the rest api code to add user to a SharePoint group using rest api. We can put the below Rest API code inside a Script editor web part or content editor web part in a web part page in SharePoint.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(2544)/users";
$.ajax({
url: requestUri,
type: "POST",
data:JSON.stringify({‘__metadata’: { ‘type’: ‘SP.User’ }, ‘LoginName’: ‘domain\\username’}),
headers: {
"accept":"application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#_REQUESTDIGEST").val()
},
success: onSuccess,
error: onError
});
function onSuccess(data) {
alert(‘User Added’);
}
function onError(error) {
alert(JSON.stringify(error));
}
});
});
</script>

<input type="button" id="btnClick" value="Add User to SharePoint Group"/>

Get All Users from SharePoint group using Rest API

Here we will see how to get all users from a SharePoint permission group using REST API. We can put the below Rest API code inside a Script editor web part or content editor web part in a web part page in SharePoint.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
$("#btnClick").click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups(2544)/users";
$.ajax({
url: requestUri,
type: "GET",
headers: {
"accept":"application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":$("#_REQUESTDIGEST").val()
},
success: onSuccess,
error: onError
});
function onSuccess(data) {
var items = data.d.results;
var results=";
for (var i = 0; i < items.length; i++) {
results+=items[i].LoginName+ " : " + items[i].Email +'\n';
}
alert(results);
}
function onError(error) {
alert(JSON.stringify(error));
}
});
});
</script>

<input type="button" id="btnClick" value="Get All Users From SharePoint Group"/>

You may like following SharePoint rest api tutorials:

Hope this SharePoint tutorial explains, how to add user to SharePoint group using rest api as well as how to retrieve all users from SharePoint group using rest api.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

  • […] Rest API: Add user to the SharePoint group and Retrieve all users from SharePoint […]

  • >