jeudi 12 février 2015

when check whether the user is in a group via javascript - get_current gives error and it calles 2 times the function

I've seen many posts about this, but I still have issues with implementing this. I googled and find a post on this site and following the SharePoint 2013 CSOM approach. http://ift.tt/1CkJpHg


Is it because of angular, but it does't work and gives error. The break point first hits "SP.SOD....) statement, which is good I think, - then it goes to SP.clientContext.get_current(). I have type SP in Console and it also works, which is good I think. - But then I get exception "Cannot read property 'get_current' of undefined" So get_current method can't be find :( - Then it goes back again in the IsCurrentUserMemberOfGroup function --> here at the 2nd time IT LOADS the get_current() method!!!... - Then it goes onfailure function it, says ""Group cannot be found." while the group is there.


so recap: problem 1: fucntion IsCurrentMemberOfGroup(...) is called 2 times, 1st time error. problem 2: the group Approvers is there it says group not found....


Any idea colleaugues ?



angular.element(document).ready(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', IsCurrentUserMemberOfGroup);
});



IsCurrentUserMemberOfGroup("Approvers", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
//...
}
});


function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

var currentContext = 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(groupName);
currentContext.load(group);

var groupUsers = group.get_users();
currentContext.load(groupUsers);

currentContext.executeQueryAsync(OnSuccess,OnFailure);

function OnSuccess(sender, args) {
console.log("OnSuccess",sender, args);

var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}

function OnFailure(sender, args) {
console.log("onFailure",sender, args);
OnComplete(false);
}


}


Aucun commentaire:

Enregistrer un commentaire