Pages

How to check user is a member of SharePoint Groups using JavaScript in SharePoint Online O365

The easiest way to check user access to a given site is to check their individual user access by using “Check Permissions” command.
To do this: Click on Gear Icon > Site Settings Under User and Permissions, choose Site Permissions .

In the top ribbon, choose Check Permissions In the User field, type the user’s name and click Check Now.
Below example is programmatic ally check user is part of group and find the snippet.

/CALL THIS FUNCTION IN YOUR DOCUMENT READY

IsCurrentUserMemberOfGroup("Group A", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {        
        CONSOLE.LOG("uSER IS MEMBER OF GROUP");
    }
 
//Main section

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

        var context = new SP.ClientContext.get_current();
        var currentWeb = context.get_web();

        var currentUser = context.get_web().get_currentUser();
        context.load(currentUser);

        var allGroups = currentWeb.get_siteGroups();
        context.load(allGroups);

        var group = allGroups.getByName(groupName);
        context.load(group);

        var groupUsers = group.get_users();
        context.load(groupUsers);

        context.executeQueryAsync(
                function(sender, args) {
                   var userInGroup = IsUserInGroup(currentUser,group);         
                   OnComplete(userInGroup);
                },
                function OnFailure(sender, args) {
                   OnComplete(false);
                }
        );

        function IsUserInGroup(user,group)
        {
            var groupUsers = group.get_users();
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == user.get_id()) {
                    userInGroup = true;
                    break;
                }
            }
            return userInGroup;
        }
}
 
 

Gowtham

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment