I am using this code example to redirect a user based on group, but I am facing this error message:
TypeError: allGroups.getByName is not a function
my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script type="text/javascript" src="/_layouts/1033/init.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.js">
</script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {
alert('IsCurrentUserMemberOfGroup');
console.log('CheckCurrentUerGroup called');
var currentContext = new SP.ClientContext.get_current();
console.log(currentContext );
var currentWeb = currentContext.get_web();
var currentUser = currentContext.get_web().get_currentUser();
console.log(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) {
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) {
OnComplete(false);
}
}
function CheckCurrentUerGroup()
{
console.log('CheckCurrentUerGroup called')
alert('CheckCurrentUerGroup called');
IsCurrentUserMemberOfGroup("Students", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
//Redirect to a certain page
alert('ifCurrentUserinGroup');
window.location = "https://www.youtube.com";
}
});
}
function testExecFunc() {
alert('testFunc');
}
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CheckCurrentUerGroup());
</script>
How can I resolve this? Have been looking around for a while but not been successful. If i need to provide more info, please note