<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(LoadGroups, "sp.js");
var group;
var users;
var ctx;
var groupCollection;
var user;
var currentuser;
//Add SharePoint Group id into below array
var userGroupIDs = [5,3];
var redirectPage = true;
var currentIndex = 0;
function LoadGroups() {
IsUserExists(userGroupIDs[0]);
}
function IsUserExists(groupID) {
ctx = SP.ClientContext.get_current();
groupCollection = ctx.get_web().get_siteGroups();
currentuser = ctx.get_web().get_currentUser();
group = groupCollection.getById(groupID);
ctx.load(group);
ctx.load(currentuser);
ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetGroupSuccess), Function.createDelegate(this, OnFailure));
}
function OnGetGroupSuccess() {
users = group.get_users();
ctx.load(users);
ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetuserSuccess), Function.createDelegate(this, OnFailure));
}
function OnGetuserSuccess() {
debugger
currentIndex++;
var userEnumerator = users.getEnumerator();
while (userEnumerator.moveNext()) {
var user = userEnumerator.get_current();
if (user.get_id() == currentuser.get_id()) {
redirectPage = false;
//alert("User Exists");
break;
}
else {
}
}
if (currentIndex == userGroupIDs.length) {
if (redirectPage) {
redirectLandingPage();
}
}else{
IsUserExists(userGroupIDs[currentIndex]);
}
}
function OnFailure(sender, args) {
//alert("Failed to execute IsCurrentUserMemberOfGroup method");
currentIndex++;
if (currentIndex == userGroupIDs.length) {
if (redirectPage) {
redirectLandingPage();
}
}else{
IsUserExists(userGroupIDs[currentIndex]);
}
}
function redirectLandingPage() {
window.location.href = "https://contoso";
}
</script>