sharepoint javascript collection not initialized error -
i have strange problem. occurs totally randomly, have no idea why , in circumstances comes.
details: want members of group executequeryasync
function. in callback userenumerator = users.getenumerator();
row throws exception: the collection has not been initialized. has not been requested or request has not been executed. may need explicitly requested.
there no other async code running. dont know if important, happens if running @ page load.
i insert code 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 knows , shares information this. saw this question well.
you try different approach of loading group users. since group client object exposes users property, load group users property initialized 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 added
sp.clientcontext.executequeryasync
Comments
Post a Comment