Check if user exists in SharePoint 2010 Group JSOM JavaScript

When we need to check whether the user exists in a group using JSOM we can use the below function.
This function takes group id as parameter which can be easily extracted from the group url.The second parameter is success function which is a callback.The usage is described below the function


function isCurrentUserMemberOfGroup(groupId, OnComplete) {

        var context = SP.ClientContext.get_current();

        var currentUser = context.get_web().get_currentUser();
        context.load(currentUser);
        var group = context.get_web().get_siteGroups().getById(groupId);
        context.load(group,'Users');
        context.executeQueryAsync(OnSuccess,OnFailure);

        function OnSuccess(sender, args) {

            var userInGroup = false;
            var e = group.get_users().getEnumerator();
            while (e.moveNext()) {
                var groupUser = e.get_current();
                if (groupUser.get_id() == currentUser.get_id()) {
                    userInGroup = true;
                    break;
                }
            }  
            OnComplete(userInGroup);
        }

        function OnFailure(sender, args) {

            OnComplete(false);
        }    
}


//Usage

isCurrentUserMemberOfGroup(groupIds.Requestorgroup ,function(userInGroup){

if(userInGroup)
{
// alert("is a member");
}

});

Comments

Popular posts from this blog

Install Node.js without admin rights

Create a lean React Solution using Typescript

Replace all occurence of String in JavaScript