1

The below code is the to remove a user from the SharePoint user group, the API call returns a 200 but the user not getting from removed from the SharePoint user group.

removeUserFromUserGroup(email): void {
const listUrl = `/_api/Web/SiteGroups/GetByName('Team WhatIf')/users/removeByLoginName`;
let url = this.sharepointService.getappUrl() + listUrl;
url = this.sharepointService.targetUrl(url);

const h = new HttpHeaders({
  Accept: 'application/json;odata=verbose'
});

this.httpClient.post
  (`${this.sharepointService.getappUrl()}/_api/contextinfo`, {}, { headers: h })
  .toPromise().then(
    (res: any) => {
      const dig = res.d.GetContextWebInformation.FormDigestValue;
      console.log(dig, 'Digest valuee');

      const body = {
        // __metadata: { type: 'SP.User' }, // table name
        loginName: `i:0#.f|membership|${email}`
        // // 'Title':'test', //values
        // // 'Test':'test'
      };

      const headers = new HttpHeaders({
        Accpet: 'application/json;odata=verbose',
        'content-type': 'application/json;odata=verbose',
        'X-RequestDigest': dig
        // 'IF-MATCH': '*',
        // 'X-HTTP-Method': 'DELETE'
      });

      const option = { headers };
      this.httpClient.post(url, JSON.stringify(body), option).subscribe(r => {
        console.log(r, 'success');
        // this.toasterService.success('Evidence Submitted Succesfully', 'Hooray!!', { positionClass: 'toast-top-right' });
      }
      );
    }); }

The response I get is 200. Please, someone, help me out with this

1 Answer 1

1

The problem in your code is, you are referencing removeByLoginName but you are passing email ID to delete the user. so try the below code,

Deleting user by Email ID:

'use strict';  
var hostweburl;  
var appweburl;  
// Get the URLs for the app web the host web URL from the query string.  
$(document).ready(function()  
{  
    //Get the URI decoded URLs.  
    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));  
    // Resources are in URLs in the form:  
    // web_url/_layouts/15/resource  
    // Load the js file and continue to load the page with information about the folders.  
    // SP.RequestExecutor.js to make cross-domain requests  
    $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js", getuser);  
});  
function getuser()   
{  
    var executor;  
    var userEmail = "[email protected]";  
    // Initialize the RequestExecutor with the app web URL.  
    executor = new SP.RequestExecutor(appweburl);  
    executor.executeAsync({  
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/sitegroups(6)/users/getbyemail('" + userEmail + "')?@target='" + hostweburl + "'",  
        method: "POST",  
        headers: {  
            "X-HTTP-Method": "DELETE"  
        },  
        success: function(data) {  
            alert("User Deleted successfully in SharePoint Group");  
        },  
        error: function(err) {  
            alert("error: " + JSON.stringify(err));  
        }  
    });  
}  
//Utilities  
// Retrieve a query string value.  
// For production purposes you may want to use a library to handle the query string.  
function getQueryStringParameter(paramToRetrieve) {  
    var params = document.URL.split("?")[1].split("&");  
    for (var i = 0; i < params.length; i = i + 1) {  
        var singleParam = params[i].split("=");  
        if (singleParam[0] == paramToRetrieve) return singleParam[1];  
    }  
}  

Deleting user by Login Name:

<script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
<script language="javascript" type="text/javascript">  
    $(document).ready(function() {  
        var removeUserUrl = "/_api/web/sitegroups/getbyname('Employee')/users/removeByLoginName";  
        var metadata = {  
            'loginName': 'i:0#.w|SharePointHOL\\KarthikJagan'  
        };  
        removeUser(removeUserUrl, metadata)  
    });  

    function removeUser(removeUserUrl, metadata) {  
        $.ajax({  
            url: _spPageContextInfo.webAbsoluteUrl + removeUserUrl,  
            type: "POST",  
            headers: {  
                "accept": "application/json;odata=verbose",  
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
                "content-Type": "application/json;odata=verbose"  
            },  
            data: JSON.stringify(metadata),  
            success: function(data) {  
                console.log(data);  
                console.log("User has been successfully removed from the SharePoint Group.");  
            },  
            error: function(error) {  
                alert(JSON.stringify(error));  
            }  
        });  
    }  
</script>  

For more information, kindly have a look at the below links,

  1. https://www.c-sharpcorner.com/article/add-and-remove-users-from-security-group-in-sharepoint-2016-using-rest-api/

  2. https://social.msdn.microsoft.com/Forums/office/en-US/1c70bfb1-039d-43cd-94b5-6138893bde8b/rest-api-removedelete-sp-user-from-group?forum=sharepointdevelopment

  3. https://www.c-sharpcorner.com/UploadFile/91b369/how-to-delete-users-from-the-group-in-sharepoint-using-rest/

1
  • Thank you Karthik it did the trick
    – Harrish
    Nov 22, 2019 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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