0

on save button click of my newsplist newform.aspx, i need to remove all the users from the custom spgroup. i tried with REST API, but there is no method to delete all users from the group! its allows us to remove only one user at a time.

how to remove all the users from the SPGroup on click of button

function PreSaveAction()
 {
      .....

   function removeGroupMember() {
    // fetch the site here 
     $.ajax(
   {
     'url': urlTemplate,
     'method': 'POST',
     'data': JSON.stringify({
         'loginName': 'i:0#.w|domain\\user'
     }),
     'headers': {
         'accept': 'application/json;odata=verbose',
         'content-type': 'application/json;odata=verbose',
         'X-RequestDigest': $('#__REQUESTDIGEST').val()
     },
     'success': function (data) {
         var d = data;
     },
     'error': function (err) {
         alert(JSON.stringify(err));
      }
  }
   );
  }
1

The following code for your reference, modify the "groupName" in the code and add the code into a script editor web part in site page.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveAction(){
    var groupName="Approvers";
    var notDeleteUsers=["user2@email.com","user3@email.com"];
    getGroupUsers("/_api/web/sitegroups/getbyname('"+groupName+"')/users?$select=Email").done(function(data){            
        data.d.results.forEach(function(user){
            var email = user.Email;
            if(!notDeleteUsers.includes(email)){
                deleteUser("/_api/web/sitegroups/getbyname('"+groupName+"')/users/getbyemail('"+email+"')").done(function(d_data){
                    //deleted user.
                    //console.log(JSON.stringify(d_data));
                });
            }           
        });
    });
    return true;
}
function getGroupUsers(url){
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        }
    });
}
//Delete user
function deleteUser(url) {
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + url,
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*",
            "X-HTTP-Method": "DELETE"
        }
    });
}
</script>
  • am trying to test the code and will revert soon – SaMolPP Mar 1 at 9:43
  • i facing one issue here : ,i have the requirement of removing all the users from that specific spgroup on click of save button as a initial step and then , am reading/fetching the value from t – SaMolPP Mar 6 at 8:34
  • i am facing one issue here : I have the requirement of (1)removing all the users from that specific spgroup on click of save button as the first step (2)then,am reading/fetching the values from the people picker control-inserting into the spgroup.(3) the issue,am facing is,assuming am having user1,user2 & myAccountID is present in SPGroup already, am populating in the people picker on document.ready(). (4) assume, i dont want to retain users user1,user2,and have to add new user4 & retain myAccountID. (5) when i clicked on save, all the users been removed. – SaMolPP Mar 6 at 8:45
  • also, is it possible to, not to remove 2 / 3 users from the group, by hardcoding and checking their email..( i dont want to remove all the users from the group,i wanna retain 2 users who always have to be there .. ) – SaMolPP Mar 6 at 8:53
  • 1
    I modify the code in the reply, set the 2/3 users email in the array "notDeleteUsers". – LZ_MSFT Mar 7 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.