SharePoint 2010: JavaScript Code To Check if user exists in a group (ECMA)

//Call this function where you want
IsCurrentUserMemberOfGroup("GropuName", function (isCurrentUserInGroup) {
                if(!isCurrentUserInGroup)
                { 
                }               
            });
//Below is the Function to Check whether the user exists in the group or not
function IsCurrentUserMemberOfGroup(strGroupName, functionComplete) {
 
            //Setup Vars
            currentContext  = null;
            currentWeb  = null;
            allGroups   = null;
            leaderGroup     = null;
            currentUser     = null;
            groupUsers  = null;
 
            //Get an instance of the Client Content.
            currentContext = new SP.ClientContext.get_current();
 
            //Grab the client web object.
            currentWeb = currentContext.get_web();
 
            //Get the current user object
            currentUser = currentContext.get_web().get_currentUser();
            currentContext.load(currentUser);
 
            //Setup the groupColletion.
            allGroups = currentWeb.get_siteGroups();
            currentContext.load(allGroups);
 
            //Now populate the objects above.
            currentContext.executeQueryAsync(
                Function.createDelegate(this, GetAllGroupsExecuteOnSuccess),
                Function.createDelegate(this, ExecuteOnFailure)
            );
 
            // GroupCollection - Load - SUCCESS
            function GetAllGroupsExecuteOnSuccess(sender, args) {
 
                // CHECK THE GROUPS
                // Time to Enumerate through the group collection that was returned.
                var groupEnumerator = allGroups.getEnumerator();
 
                // Loop for the collection.
                while (groupEnumerator.moveNext()) {
 
                    //Grab the Group Item.
                    var group = groupEnumerator.get_current();
                    if (group.get_title().indexOf(strGroupName) > -1) {
 
                        // Now that we have the group let's grab the list of users.
                        groupUsers = group.get_users();
                        currentContext.load(groupUsers);
                        currentContext.executeQueryAsync(
                            Function.createDelegate(this, SingleGroupExecuteOnSuccess),
                            Function.createDelegate(this, ExecuteOnFailure)
                        );
                    }
                }
            }
 
            // Single Group - Load - SUCCESS
            function SingleGroupExecuteOnSuccess(sender, args) {
 
                // Time to setup the Enumerator
                var groupUserEnumerator = groupUsers.getEnumerator();
 
                // This is the flag to set to true if the user is in the group.
                var boolUserInGroup = false;
 
                // and start looping.
                while (groupUserEnumerator.moveNext()) {
 
                    //Grab the User Item.
                    var groupUser = groupUserEnumerator.get_current();
 
                    // and finally. If a Group User ID Matches the current user ID then they are in the group!
                    if (groupUser.get_id() == currentUser.get_id()) {
                        boolUserInGroup = true;
                    }
                }
 
                //Run the delegate function with the bool;
                functionComplete(boolUserInGroup);
            }
 
            // GroupCollection or Single Group - Load - FAILURE
            function ExecuteOnFailure(sender, args) {
                //Run the delegate function and return false because there was no match.
                functionComplete(false);
            }
        }

Comments

Popular posts from this blog

How to copy DLLs from GAC.