Вопрос

I have a strange problem. It occurs totally randomly, I have no idea why and in what circumstances it comes.

Details: I want to get the members of a Group with the executeQueryAsync function. In the callback the userEnumerator = users.getEnumerator(); row throws this exception: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

There is no other async code running. I dont know if this important, but this only happens if this is running at page load.

I insert this code with an XML Viewer webpart.

    var ctx = SP.ClientContext.get_current(),

    groups = ctx.get_web().get_siteGroups(),
    group = groups.getById(6),
    users = group.get_users();

    ctx.load(group);
    ctx.load(users);

    ctx.executeQueryAsync(function () {
        var userEnumerator,
            user;

         $("#members-select").empty();

         userEnumerator = users.getEnumerator();
         while (userEnumerator.moveNext()) {
              user = userEnumerator.get_current();
              $("#members-select").append('<option>' + user.get_title() + '</option>');
          }
    });

Thanks, if anyone knows and shares any information about this. I saw this question as well.

Это было полезно?

Решение

You could try a slightly different approach of loading group users. Since Group client object exposes Users property, you could load Group with Users property initialized like this:

ctx.load(group,'Users');

Example:

(function(){
  var ctx = SP.ClientContext.get_current();
  var groups = ctx.get_web().get_siteGroups();
  var group = groups.getById(6);

  ctx.load(group,'Users');
  ctx.executeQueryAsync(function () {

       var users = group.get_users();
       var e = users.getEnumerator();
       while (e.moveNext()) {
        var user = e.get_current();
        console.log(user.get_title());
       }

     },
     function(sender,args){
       console.log(args.get_message());    
     }
   );

})();

Key points:

  • Error handler was added for SP.ClientContext.executeQueryAsync
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top