3

I have a SharePoint group name "Admin".

Can someone provide me JavaScript code to check if the current logged in user belongs to this group?

I have a query which I want to run based on this condition.

  • Hi Robert, I could not enter long text which include code in comment box, there is limitation to that – Brishal May 19 '15 at 10:13
3
function CheckMemberInAdminGroup() {    
    var clientContext = new SP.ClientContext.get_current();
    this.currentUser = clientContext.get_web().get_currentUser();
    clientContext.load(this.currentUser);

    this.userGroups = this.currentUser.get_groups();
    clientContext.load(this.userGroups);
    clientContext.executeQueryAsync(success, failure);
}

function success() {
         var groupsEnumerator = this.userGroups.getEnumerator();
          while (groupsEnumerator.moveNext()) {
             var group = groupsEnumerator.get_current();               
             if(group.get_title() == "Admin") {
                 // User is member of Admin group, do stuff
             }
          }
}

function failure() {
          // Something went wrong with the query
}
  • Thanks, do i need to upload any js file? i yes please provide me the file name also – Brishal May 14 '15 at 14:33
  • What do you mean? You will need to add this JavaScript onto the page, either by pointing to a .js file or adding the JavaScript into a Content Editor Web Part. You don't need any third party libraries, if that's what you're asking. – Submits May 14 '15 at 14:34
  • Just a note, you'll need to use something like ajax createDelegate or jquery $.proxy to pass the "this" object: clientContext.executeQueryAsync($.proxy(this, "success"), $.proxy(this,"failure")); – Anders Aune May 14 '15 at 22:29
1

Check if user is member of group using JSOM:

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

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

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

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

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

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

        currentContext.executeQueryAsync(OnSuccess,OnFailure);

        function OnSuccess(sender, args) {
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == currentUser.get_id()) {
                    userInGroup = true;
                    break;
                }
            }  
            OnComplete(userInGroup);
        }

        function OnFailure(sender, args) {
            OnComplete(false);
        }    
}

Usage:

function IsCurrentUserHasContribPerms() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        // The current user is in the [Members] group!
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.