getting TypeError: allGroups.getByName is not a function
-
08-02-2021 - |
Pergunta
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
Solução
Try Using below SharePoint REST API Code:
function isCurrentUserMemberOfGroup(groupName) {
var isUserInGroup = false;
$.ajax({
async: false,
headers: { "accept": "application/json; odata=verbose" },
method: "GET",
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser/groups",
success: function (data) {
data.d.results.forEach( function (value) {
if (value.Title == groupName) {
isUserInGroup = true;
}
});
},
error: function (response) {
console.log(response.status);
},
});
return isUserInGroup;
}
How to Use:
var isAdmin = isCurrentUserMemberOfGroup("Site Owners");
if(isAdmin) {
window.location = "http://www.yourSiteOwnerurl.com"; // If user is a Site Owner
} else {
window.location = "http://www.yourSiteMemberurl.com"; // If user is not a Site Owner
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange