Wednesday, December 9, 2015

How to check if the currently logged-in user belongs to a group in SharePoint Online using REST

Hi Guys,

If you want to check that the currently logged-in user belongs to a group please find out how to do this using my code below, hopefully it will help you a lot!

var groupName;
function getCurrentUserPermission(userID) {
    $.ajax
    ({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + userID + ")/Groups",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        dataType: "json",
        async: true,
        success: function (data) {
            /* get all group's title of current user. */
            for (var i = 0; i < data.d.results.length; i++) {
                if (data.d.results[i].Title == "Users") {
                    groupName = data.d.results[i].Title;
                  //do your hide/and show activity
                }
                else if (data.d.results[i].Title == "Admin") {
                    groupName = data.d.results[i].Title;
                   //do your hide/and show activity
                }
                else if (data.d.results[i].Title == "Operation") {
                    groupName = data.d.results[i].Title;
                   //do your hide/and show activity
                }
            }
        }
    });
}

Happy Coding !!

Thanks

Pankaj Sriavstava


by Pankaj Srivastava via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment