Check Whether user belongs to SharePoint Group using REST api

How to Check Whether user belongs to SharePoint Group using REST api

Code - 


$(document).ready(function(){
    isOwnerGroupMember();
 });

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

By using above code you can check whether logged in user is member of the SharePoint Group or not. You can apply your own logic based on your business requirement.


Comments

Popular posts from this blog

How to Customize exported excel's cell background color in Datatables

Populate dropdown using jquery ajax in SharePoint

Customizing SharePoint list and implementing Quick search using jQuery