Add Bulk Users to SharePoint Group from Excel using Rest API

This SharePoint Rest API tutorial, we will discuss, how to add bulk users to SharePoint group from Excel using the SharePoint REST API. The same Rest API code, we can use to add bulk users to the SharePoint group.

Add Bulk Users to SharePoint Group from Excel using Rest API

Recently we have a requirement to add bulk users to a SharePoint group. We have the users in one excel sheet.

In the excel sheet we have only the username like below:

  • bksahoo
  • bijay
  • sahoo like this.

And we have uploaded the excel sheet to the SharePoint SiteAssets library and then we write the code read the excel from SiteAssets library.

Below is the code to add bulk users to SharePoint groups:

<input type=button id="btnClick" value='Click Here' onclick="AddUserFromExcel();" />

<script language="javascript" type="text/javascript">
var i;
var l;
var a1;
var user;
var spGroup;

function AddUserFromExcel()
{
l=1;
var Excel;
Excel = new ActiveXObject(“Excel.Application");
Excel.Visible = false;
for(l=1;l<3;l++)
{
a1=Excel.Workbooks.Open(“http://SiteURL/SiteAssets/Users.xlsx?Web=1").ActiveSheet.Cells(l,1).Value;
var a4="Domain\\"+a1;
var clientContext = new SP.ClientContext.get_current();
var web=clientContext.get_web();
var siteGroups = clientContext.get_web().get_siteGroups();
spGroup=siteGroups.getById(4990);
user=web.ensureUser(a4);
var userCollection=spGroup.get_users();
userCollection.addUser(user);
clientContext.load(user);
clientContext.load(spGroup);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded()
{
alert('success');
}
function onQueryFailed()
{
alert('Request failed.');
}
}
}
</script>

You may like following SharePoint Rest API tutorials:

This SharePoint tutorial, we learned how to add bulk users to SharePoint group using Rest API.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>