1

I would like to redirect based on a users group membership. First get the users groups and then have a switch statement to redirect based on 1 of 5 groups.

Below is my code I get to alert that says I get this far then nothing happens in UI

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
});

var context = null; 
var web = null; 
var currentUser = null;
var userGroups = null;
var displayGroups = null;

function getWebUserData() {

    context = new SP.ClientContext.get_current();   // Get the current user 
    web = context.get_web();                        // Get the current web    
    currentUser = web.get_currentUser();            // Get the current user 
    context.load(currentUser);                      // Load the current user 

    userGroups = currentUser.get_groups();
    context.load(userGroups);

    alert('I get this far then nothing!');

    var groupsEnumerator = userGroups.getEnumerator();
    alert('var groupsEnumerator ');

    while (groupsEnumerator.moveNext()) {
        var group = groupsEnumerator.get_current();
        displayGroups += group.get_title() + ' \n ';
    }
    context.executeQueryAsync(onSuccessMethod, onFailureMethod);
}

function onSuccessMethod() {
    alert(displayGroups);
    alert('User name:' + currentUser.get_title() + '\n Login Name:' + currentUser.get_loginName());

}

function onFailureMethod(sender, args) {

    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

}
0

2 Answers 2

1

You are trying to operate with groups before actually receiving data from the server.

The code which executes the query to the server and fetches your group is context.executeQueryAsync.

So you need to put your logic around groups iteration inside onSuccessMethod - when the groups data received:

$(document).ready(function () {
    // Wait until SP.JS has loaded before calling getWebUserData 
    ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
});

var context = null; 
var web = null; 
var currentUser = null;
var userGroups = null;
var displayGroups = null;

function getWebUserData() {

    context = new SP.ClientContext.get_current();   // Get the current user 
    web = context.get_web();                        // Get the current web    
    currentUser = web.get_currentUser();            // Get the current user 
    context.load(currentUser);                      // Load the current user 

    userGroups = currentUser.get_groups();
    context.load(userGroups);

    alert('I get this far then nothing!');


    context.executeQueryAsync(onSuccessMethod, onFailureMethod);
}

function onSuccessMethod() {
    var groupsEnumerator = userGroups.getEnumerator();
    alert('var groupsEnumerator ');

    while (groupsEnumerator.moveNext()) {
        var group = groupsEnumerator.get_current();
        displayGroups += group.get_title() + ' \n ';
    }
    alert(displayGroups);
    alert('User name:' + currentUser.get_title() + '\n Login Name:' + currentUser.get_loginName());

}

function onFailureMethod(sender, args) {

    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

}
5
  • Will this find security groups? I created a security group and added myself but when the alert(displayGroups) shows the value is null. I am also site admin. Thanks!
    – Tony Danza
    Nov 23, 2016 at 14:37
  • Nope, it only returns SharePoint groups user belongs to. Nov 23, 2016 at 14:51
  • Is there away to get security groups? or can i add sharepoint groups?
    – Tony Danza
    Nov 23, 2016 at 14:58
  • @TonyDanza if the answer is resolved your particular issue, please don't forget to mark an answer as Accepted. Nov 23, 2016 at 19:39
  • From javascript it's not possible to get security groups. Nov 25, 2016 at 10:43
1

You can use the JQuery library SPServices (https://spservices.codeplex.com/) to test for a user's group membership. I've used this code several times to check for group membership, then you can put in your switch statement or redirect in depending on the group the person is in.

Hope this helps.

<script src="http://yourpath/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        isGroupMember("YOUR_GROUP_NAME", function(result) {
            if (result) {
                alert("is member");
            } else {
                alert("Is not member");
            }
        });


        function isGroupMember(groupName, callback) {
            $().SPServices({
                operation: "GetGroupCollectionFromUser",
                userLoginName: $().SPServices.SPGetCurrentUser(),
                async: true,
                completefunc: function(xData, Status) {

                    callback(!!($(xData.responseXML).find("Group[Name='" + groupName + "']").length)); /* returns bool */
                }
            });
        };

    });
</script>

Your Answer

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

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