Forked from vgrem/SP.IsCurrentUserMemberOfGroup.Example.js
Created
July 6, 2016 16:12
-
-
Save SriharshaShesham/b6241569681fc48adf332d5e8ad375c7 to your computer and use it in GitHub Desktop.
Determine if current user is a member of Group via CSOM(JavaScript) in SharePoint 2010/2013
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Usage | |
function IsCurrentUserWithContributePerms() | |
{ | |
IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) { | |
if(isCurrentUserInGroup) | |
{ | |
// The current user is in the [Members] group | |
} | |
}); | |
} | |
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserWithContributePerms, 'SP.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function IsCurrentUserMemberOfGroup(groupName, OnComplete) { | |
var context = new SP.ClientContext.get_current(); | |
var currentWeb = context.get_web(); | |
var currentUser = context.get_web().get_currentUser(); | |
context.load(currentUser); | |
var allGroups = currentWeb.get_siteGroups(); | |
context.load(allGroups); | |
var group = allGroups.getByName(groupName); | |
context.load(group); | |
var groupUsers = group.get_users(); | |
context.load(groupUsers); | |
context.executeQueryAsync( | |
function(sender, args) { | |
var userInGroup = IsUserInGroup(currentUser,group); | |
OnComplete(userInGroup); | |
}, | |
function OnFailure(sender, args) { | |
OnComplete(false); | |
} | |
); | |
function IsUserInGroup(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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment