sharepoint javascript collection not initialized error

我只是一个虾纸丫 提交于 2019-12-25 02:14:27

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/22607974/sharepoint-javascript-collection-not-initialized-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!