0

Lets say i have a function that has a for loop in it. I need to loop it becouse i have an array that i need to get items dynamically from: newsArray[i].Group.

In the function Success , i will be checking if the user is in the group then it will show a popup for that user.

The executeQueryAsync is not working in a for loop, how can i acchieve this?

function ShowDialog(){
    for(var i = 0; i < newsArray.length; i++){

       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(newsArray[i].Group);

       currentContext.load(group);
       var groupUsers = group.get_users();
       currentContext.load(groupUsers);
       context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, failed)); 
       }

      function success(sender,args){

        //show a popup if user is in group
      }   
2

You don't need to loop through all groups, instead you can get the groups for the current user like this:

var groups = currentContext.get_web().get_currentUser().get_groups();

It does seem spoonfeeding is the preferred approach, so I'll just leave this here:

function filterNewsArrayByCurrentUser(newsArray, callback) {
  $.ajax({
    url: '/_api/Web/CurrentUser/Groups?$select=Title',
    headers: {
      Accept: 'application/json;odata=verbose'
    },
    success: function(d) {
      newsArray = $(newsArray).filter(function(i, na) { 
        return $(d.d.results).filter(function(j, r) {
          return r.Title === na.Group;
        }).length > 0;
      });
      callback(newsArray.toArray());
    }
  });
}

//Usage:
filterNewsArrayByCurrentUser(newsArray, function(newsArray) {
  console.log(newsArray);
 });
  • Actually, this might just work for SharePoint 2013. As you didn't specify the version you use I'll just leave this here – eirikb Apr 10 '14 at 15:03
  • 1
    It's tagged SharePoint Online, so it is indeed 2013 I persume – Robert Lindgren Apr 10 '14 at 15:27
  • Well look at that. I'm blind as a bat – eirikb Apr 10 '14 at 15:31
  • hi, thanks for answear, its sharepoint 2013 online – Anders Apr 11 '14 at 7:25
  • 1
    @Jarule now I have updated the example so it should fit you 100%. And I used jQuery, so then I guess I should apologize to Aveenav as I thought one didn't need to - of course one should not need to make the example this comlpicated either. Actually, I think I might use underscorejs.org/#intersection instead – eirikb Apr 15 '14 at 9:15
2

@ericb: Excellent suggestion!

ArrayOfGroups = new Array("Owner", "Member", "Visitors", "Trainers"); //could be dynamic array of group names

var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var userGroups = web.get_currentUser().get_groups(); //as suggested by ericb

ctx.load(userGroups);
ctx.executeQueryAsync(
    function () {
        //success
        var groupEnumerator = userGroups.getEnumerator();
        var showPopup = false; //To show one popup, user may belong to multiple groups
        while (groupEnumerator.moveNext()) {

            var group = groupEnumerator.get_current();
            var groupName = group.get_title();

            //jQuery $.inArray
            if ($.inArray(groupName, ArrayOfGroups) > -1) {
                //window.console && console.info('Group: ' + groupName);
                showPopup = true;
            }
        }

        if (showPopup == true) {
            // show Popup
            // Implement your logic here
        }
    },
    function () {
        //error
    }
);

Alternatively, you could make use of script in the article (by Tom Wilson) to check if a user belongs to a particular group. http://styledpoint.com/blog/sharepoint-2010-check-to-see-if-user-exists-in-a-group-via-javascript-ecma/

You could trim it down to suit your needs.

IsCurrentUserMemberOfGroup("Lead", function (isCurrentUserInGroup) {
  if (isCurrentUserInGroup) {
    // The current user is in the group! Hurrah!
  }
});
  • 2
    Not to upset anyone, but I'm not quite sure how I feel about this answer, there are some points that bothers me; Spoonfeeding when another answer has the important parts already, using jQuery out of nowhere for no particularly good reason, copying the main and important gist from another answer, referencing a link for 2010 which has similar problems as the initial question (not using get_groups because it is for 2010) and lastly referencing myself with something that hardly looks like my name or my username :/ – eirikb Apr 11 '14 at 7:52
1

The executeQueryAsync is not working because it is asynchronous so the for just ends before that you have the response.
If you use recursion you can achieve it

var newsArray,
var index;
function myExecuteAsy(newsArray,index){
    if (typeOf(newsArray[index])=="undefined"){
        return;
    }
    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(newsArray[index].Group);

    currentContext.load(group);
    var groupUsers = group.get_users();
    currentContext.load(groupUsers);
    context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, failed)); 
}

function success(sender,args){
    //show a popup if user is in group
    myExecuteAsy(newsArray,index+1);
}  

start with the call

myExecuteAsy(newsArray,0)

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.