4

I have copied some snippets of code from here

to test if a user belongs to a certain group, I am using Sharepoint 2010, however I am getting an method is undefined error.

I am making sure that sp.js is loaded with :

$(document).ready(function () {
    ExecuteOrDelayUntilScriptLoaded(removeLink, "sp.js");
});

then when this is loaded I check if the user is a current member:

    function removeLink(){
        IsCurrentUserMemberOfGroup("Approvers", function (isCurrentUserInGroup){
        if(isCurrentUserInGroup)
   {
    alert(isCurrentUserInGroup);
   }
   else
   {
       alert(isCurrentUserInGroup + "- no");
   }
        });

    }

The isCurrentUserMemberOfgroup method looks like this:

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);   //ERROR HERE
        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);
        }    
}

However this line causes an error:

        var group = allGroups.getByName(groupName);   //ERROR HERE

allGroups is defined, however it contains no method getByName()

1
  • I'm still not sure why it is not defined, but I believe group is a pre-defined word in Sharepoint API, is it not? Might want to rename your group variable
    – Brandon C.
    Commented Jun 25, 2014 at 7:10

1 Answer 1

6

what version of sharepoint you using ?

.getByName() 

This is not in 2010 and was introduced in 2013.

The only thing i know of in 2010 that uses getbyname is getting the web role definition.

get_web().get_roleDefinitions().getByName("Read Only Access");

im sure there are other things that use it but what your looking for is only applicable in 2013 the getting of groups by name .getByName().

get groups by name in 2010:

public static void GetSiteGroup()
{
      ClientContext clientContext = new ClientContext("http://basesmc2008");
      Web web = clientContext.Web;
      GroupCollection groups = web.SiteGroups;


      clientContext.Load(groups, groupitems => groupitems.Include(groupitem => groupitem.Title,
        groupitem => groupitem.Description).Where(groupitem=> groupitem.Title == "Home Owners"));

      clientContext.ExecuteQuery();

}

http://social.msdn.microsoft.com/Forums/windowsazure/en-US/39f1a792-f881-464f-91ea-5e1d4aaef764/client-object-model-get-sitegroups-by-name

in 2013 your solution is valid!

if you are using 2010 than this is the solution:

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 = GetSiteGroup(groupName, currentContext, currentWeb);   
        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);
        }    
}

    public static void GetSiteGroup(var groupName, var clientContext, var web)
    {
          GroupCollection groups = web.SiteGroups;


          clientContext.Load(groups, groupitems => groupitems.Include(groupitem => groupitem.Title,
            groupitem => groupitem.Description).Where(groupitem=> groupitem.Title == groupName));

    }

done the above. but havent tested it but it should look similart to the above!

Hi All. In SP2010 I found I had to enumerate through the groups and users using javascript. Please find my code below. Hope this helps someone.

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);
        currentContext.load(allGroups, 'Include(Users)');

        currentContext.executeQueryAsync(OnSuccess,OnFailure);

        function OnSuccess(sender, args) {
                 var userInGroup = false;
            var groupEnumerator = allGroups.getEnumerator();
            while (groupEnumerator.moveNext() && !userInGroup) {
                var oGroup = groupEnumerator.get_current();
                if (oGroup.get_title() == groupName) {
                    var collUser = oGroup.get_users();
                    var userEnumerator = collUser.getEnumerator();
                    while (userEnumerator.moveNext() && !userInGroup) {
                        var oUser = userEnumerator.get_current();
                        if (oUser.get_id() == currentUser.get_id()) {
                            userInGroup = true;
                            //break;
                        }
                    }
                }
            }
            OnComplete(userInGroup);
        }

        function OnFailure(sender, args) {
            OnComplete(false);
        }    
}
2
  • no probs happy to help!
    – Ali Jafer
    Commented Jun 25, 2014 at 7:44
  • looks like its a bit of c# mixed in there ;) but I think I can work with it. thanks
    – Michael
    Commented Jun 25, 2014 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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