SharePoint Online Environment:
var isHelpDeskUser = False;
$(function() {
ValidationForHelpDesk();
});
function ValidationForHelpDesk(){
var url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups'
$.getJSON(url, function (data) {
$.each(data.value, function (key, value) {
if (value.Title == 'SAGroup') { //SharePoint HelpDesk Group name is SAGroup
isHelpDeskUser = true;
}
});
})
}
If Current User is part of "SAGroup" then variable isHelpDeskUser will set to true;
var isHelpDeskUser = False;
$(function() {
ValidationForHelpDesk();
});
function ValidationForHelpDesk(){
var url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups'
$.getJSON(url, function (data) {
$.each(data.value, function (key, value) {
if (value.Title == 'SAGroup') { //SharePoint HelpDesk Group name is SAGroup
isHelpDeskUser = true;
}
});
})
}
If Current User is part of "SAGroup" then variable isHelpDeskUser will set to true;
SharePoint On Premise environment:
function ValidationForHelpDesk(){
CurrentUserMemberOfGroup(" SAGroup", function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
console.log('user is member of the group');
isHelpDeskUser = true;
}
else
{
console.log('user is not member of the group');
isHelpDeskUser = false;
}
});
}//Function Close
function CurrentUserMemberOfGroup( groupName, OnComplete) {
var ctx = new SP.ClientContext.get_current() ;
var currentUser = ctx.get_web().get_currentUser( );
ctx.load(currentUser);
var Groups = ctx.get_web().get_siteGroups() ;
ctx.load(Groups);
var group = Groups.getByName(groupName);
ctx.load(group);
var groupUsers = group.get_users();
ctx.load(groupUsers);
ctx.executeQueryAsync(
function(sender, args) {
var userInGroup = UserInGroup(currentUser,group) ;
OnComplete(userInGroup);
},
function OnFailure(sender, args) {
OnComplete(false);
}
);
function UserInGroup(user,group)
{
var groupUsers = group.get_users();
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext( )) {
var groupUser = groupUserEnumerator.get_ current();
if (groupUser.get_id() == user.get_id()) {
userInGroup = true;
break;
}
}
return userInGroup;
}
}