Pergunta

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));
      }
  }
   );
  }
Foi útil?

Solução

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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top