Add and remove a user from SharePoint group using JavaScript

This jsom SharePoint tutorial explains, how to add current SharePoint user to a SharePoint group using the JavaScript object model. We will also see, how to remove user other than logged-in user to SharePoint group using the JavaScript object model (jsom).

Here we will see how to add the logged-in user to SharePoint group using jsom and also we will see how to add a user (other than logged in user) to SharePoint group using JavaScript in SharePoint Online or SharePoint 2013/2016.

Add user to SharePoint group using JavaScript

Below is the jsom code which we can add inside a script editor web part in a web part page in SharePoint Online/2013/2016.

<input type='button' value='Add User to SharePoint Group' onclick="AddUserToSharePointGroup();"/>
<br />

<script language="javascript" type="text/javascript">
var user;
var spGroup;
function AddUserToSharePointGroup() {

var clientContext = new SP.ClientContext.get_current();

var siteGroups = clientContext.get_web().get_siteGroups();
spGroup=siteGroups.getById(7);
user=clientContext.get_web().get_currentUser();
alert(user.email);
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>

Once you run the code, the logged-in user or the current user will be added to the SharePoint group using the JavaScript object model (jsom).

Add user other than logged-in user to SharePoint group using JavaScript object model

Now, we will see how to add a user other than the logged-in user to a SharePoint group using the JavaScript client object model (jsom).

Put the below code inside a script editor web part in a web part page in SharePoint Online or SharePoint 2013/2016.

Here we need to call the ensureUser() method to validate the user. And parameter it will take as domainname\username.

<input type='button' value='Add User to SharePoint Group' onclick="clickMethod();"/>
<br />

<script language="javascript" type="text/javascript">

var user;
var spGroup;
function clickMethod() {

var clientContext = new SP.ClientContext.get_current();

var web=clientContext.get_web();
var siteGroups = clientContext.get_web().get_siteGroups();
spGroup=siteGroups.getById(2544);
//user=clientContext.get_web().get_currentUser();
user=web.ensureUser(“domain\\username");
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>

Remove user from SharePoint group using JavaScript

Similarly, if you want to remove a user from a SharePoint group then we can use the remove() method which usually takes the user as the parameter.

Below is the full code to remove a user from a SharePoint group using JavaScript.

<input type='button' value='Remove User from SharePoint Group' onclick="RemoveUser();"/>
<br />

<script language="javascript" type="text/javascript">

var user;
var spGroup;
function RemoveUser() {

var clientContext = new SP.ClientContext.get_current();
var web=clientContext.get_web();
var siteGroups = clientContext.get_web().get_siteGroups();
spGroup=siteGroups.getById(2544);
//user=clientContext.get_web().get_currentUser();
user=web.ensureUser(“DomainName\\username");
var userCollection=spGroup.get_users();
userCollection.remove(user);
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

}

function onQuerySucceeded() {
alert(‘success');
}

function onQueryFailed() {
alert(‘Request failed.');
}

</script>

You may like following SharePoint JSOM tutorials:

In this SharePoint tutorial, we discussed, how to add logged in user as well as other users to SharePoint group using JavaScript object model (jsom) in SharePoint 2013/2016 or SharePoint Online. We will also discuss how to remove a user from a SharePoint group using jsom.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

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

>