SharePoint REST API

Hello SharePointers,
In this blog, we will be using the SharePoint REST API to check if any user is present in the SharePoint group or not.

In my recent development, there was a requirnment to check the user from perticular group and manage his access to the document Library.

So I have used a REST API to do this functionality.

For example, We will check, if the user is present in the “Owners” group or not.

/** Check if the current user is in the Owners group **/
function isOwnerGroupMember() {
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('Owners')/Users?$filter=Id eq " + _spPageContextInfo.userId,
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(data) {
            if (data.d.results.length == 0) {
                console.log("User not in group : Owners");                
            } else {
                console.log("User in group : Owners");                
            }
        },
        error: function(err) {
            console.log("Error while checking user in Owner's group");            
        }
    });
}

The above code meets the exact requirement of our coding. If you understand above code, it is well and good.

If not then don’t worriy, I have explained all details about this code as below. Please go through it and understand the code at your best.

In the above function, we are getting a response for the users present in “Owners” group and filtering users with currently logged in user id (_spPageContextInfo.userId).

If the user is present, it should provide a response with 1 or 0. We are checking this condition using if statement (data.d.results.length)

If data.d.results.length is 0, it means the user is not present in the specified permission group and if the data.d.results.length is 1, it means the user is present in the specified permission group

I hope you liked this article. If you have any queries, please use below comments section for the same.

Happy Coding…
For more reference for SharePoint REST API visit here
For more SharePoint tutorials please visit SharePointGems

2 thoughts on “SharePoint REST API: Check if the current user is present in SharePoint Group or not”
  1. I’m the manager of JustCBD label (justcbdstore.com) and am looking to broaden my wholesale side of business. It would be great if anybody at targetdomain is able to provide some guidance . I considered that the very best way to accomplish this would be to reach out to vape companies and cbd retail stores. I was hoping if anybody could suggest a reliable web site where I can get CBD Shops B2B Data List I am already reviewing creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. On the fence which one would be the most ideal choice and would appreciate any advice on this. Or would it be much simpler for me to scrape my own leads? Ideas?

Leave a Reply