Delete multiple users from SharePoint groups

Indhu Manoharan 21 Reputation points
2022-08-02T09:12:20.437+00:00

Is there a SharePoint API to remove multiple users at a time from SharePoint group?

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-08-03T01:41:02.527+00:00

    Hi @Indhu Manoharan ,
    Rest API Id parameter not available to set with multiple values. As a workaround is to get the UserCollection in the group firstly and then put the user id in an array, loop the array to get user id and remove.

    A code snippet for your reference:

    ExecuteOrDelayUntilScriptLoaded(RemoveMultipleUsers, 'sp.js');  
    var userids=[12,13];  
    function RemoveMultipleUsers()  
    {   
        var clientContext = new SP.ClientContext("http://sp/sites/teams");  
        var oWeb = clientContext.get_web()  
        var collGroup = oWeb.get_siteGroups();  
      
        var oGroup = collGroup.getById(7); //Change 7 based on the group's id  
        var oUser = oGroup.get_users();  
        console.log(oUser);  
        clientContext.load(oUser);  
        for(var i=0;i<userids.length;i++)  
        {  
           oUser.removeById(userids[i]);  
        }  
        clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));  
      
    }  
    function onQuerySucceeded(){  
      
        alert('User Removed');  
    }  
      
    function onQueryFailed() {  
        alert('Failed');  
    }  
    

    Replace all the userid which want to remove in the userids array and also the site url and group id, it will work as expected.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.