Reputation: 3
I've got a weird issue and I'm not able to trace how to fix it.
What I'm trying to do is get the user's ID and comparing it to all the users in a specific group, with the goal to find out if this user is in that group.
I believe that it should all work, but I have an issue in the OnSuccess function when I am trying to get the enumerator for the groups. The error is:
SCRIPT5022: 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.
ExecuteOrDelayUntilScriptLoaded(getUserGroup, "sp.js");
//found online
function getUserGroup(){
var context = new SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var currUser = web.get_currentUser();
context.load(currUser);
var groups = web.get_siteGroups();
context.load(groups);
context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess(groups)), Function.createDelegate(this, this.OnFailure));
}
function OnSuccess(g){
var isInGroup = false;
var groupEnumerator = g.getEnumerator();
while(groupEnumerator.moveNext() && !isInGroup){ //go through all the groups
var oGroup = groupEnumerator.get_current(); //get current group in the enumerator
if(oGroup.get_title() == "SPM Team"){ //is the group title named this?
var collUser = oGroup.get_users(); //get all the users in the group
var userEnumerator = collUser.getEnumerator(); //get the enumeration
while(userEnumerator.moveNext() && !isInGroup){ //go through all users in group
oUser = userEnumerator.get_current(); //get current user in the enumerator
if(oUser.get_id() == currUser.get_id()){ //is the user's id the same as the viewer?
isInGroup = true;
console.log("success");
}
}
}
}
}
function OnFailure(){
console.log("nope");
}
At first I thought that this was because the context.load(groups) hadn't been fully loaded in from the request from the server, but even after time-gating it for a set amount of time, it still did not work.
Any Idea as to a solution?
Upvotes: 0
Views: 30
Reputation: 5493
Here you go.
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getUserGroup, "sp.js");
//found online
function getUserGroup() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var currUser = web.get_currentUser();
context.load(currUser);
var groups = web.get_siteGroups();
context.load(groups);
context.executeQueryAsync(
Function.createDelegate(this,
function () {
var isInGroup = false;
var groupEnumerator = groups.getEnumerator();
while (groupEnumerator.moveNext() && !isInGroup) { //go through all the groups
var oGroup = groupEnumerator.get_current(); //get current group in the enumerator
if (oGroup.get_title() == "SPM Team") { //is the group title named this?
var collUser = oGroup.get_users(); //get all the users in the group
context.load(collUser);
context.executeQueryAsync(
Function.createDelegate(this,
function () {
var userEnumerator = collUser.getEnumerator(); //get the enumeration
while (userEnumerator.moveNext() && !isInGroup) { //go through all users in group
oUser = userEnumerator.get_current(); //get current user in the enumerator
if (oUser.get_id() == currUser.get_id()) { //is the user's id the same as the viewer?
isInGroup = true;
console.log("success");
}
}
}),
Function.createDelegate(this,
function (sender, args) {
console.log(args);
}));
}
}
}),
Function.createDelegate(this,
function OnFailure() {
console.log("nope");
}));
}
</script>
Upvotes: 0