To get all the groups and then check if the user exists in each group see the other post Get all Groups Javascript\Ecmascript Client object model sharepoint 2010
Or you can simple check if the user exists in one of the pre-specified groups like i did below
see example
function CheckCurrentUser()
{
var clientContext = new SP.ClientContext();
var groupCollection = clientContext.get_web().get_siteGroups();
// Get the Our Group’s ID
var _group = groupCollection.getById(2); ->> ID of the Group
var users = _group.get_users();
clientContext.load(_group);
clientContext.load(users,’Include(loginName)’);
this._currentUser = clientContext.get_web().get_currentUser(); ->> Get current user
clientContext.load(this._currentUser,’Include(loginName)’);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
}
function onQuerySucceeded() {
if(users.count >0)
{
UserExistInGroup = false;
for(var i=0; i < users.count; i++)
{
if(users[i].get_loginName() == this._currentUser.get_loginName())
{
UserExistInGroup = true;
}
}
alert('Is User In The Group' + UserExistInGroup )
}}
function onQueryFailed(sender, args) {
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
Ads by Google
Dude, if you're going to post code like this, getting it in google searches, do us all a favor and make sure it WORKS! The code above has obviously never been tested.
ReplyDeleteReplace the succeeded function with something like the following:
function onCurrUserSucceeded() {
//alert("users length"+users.count+" " +users.length);
userEnumerator = users.getEnumerator();
UserExistInGroup = false;
while (userEnumerator.moveNext()) {
i = userEnumerator.get_current();
if(i.get_loginName() == _currentUser.get_loginName())
{
UserExistInGroup = true;
alert('Is User In The Group' + UserExistInGroup );
_isEditor=true;
}
}
}
Code is fully tested and it works. All you gotta do is fix the quotes ' and "". If it didn't work for you does not mean it does not work for others. It is the most viewed and used Post on my blog
ReplyDeleteNot much use for this code since all you can do is display an alert to the user. Global variables cannot be populated and nothing can be returned to calling functions. Pretty useless!
ReplyDeleteoh C'mon Clem you gotta do something right
ReplyDeletewhat is purpose of the 'Include(loginName)'. it is giving undefined error
ReplyDeleteI'm getting following error if i replaced quotes also
ReplyDelete"Request failed. Field or property "loginName" does not exist.
undefined"
This comment has been removed by the author.
ReplyDeleteHi,
ReplyDeletei want get the all the users names from a SharePoint Group. and add to list of users names to DrowpDown list
Hey,
ReplyDeleteCouldn't get the code above to work for myself (so used a combination of examples), but this found this worked:-
var groupCollection;
var pertinent_group;
var current_user;
var isUserInGroups = false;
var context;
function FindLoggedInUser() {
context = SP.ClientContext.get_current();
var currentUser = context.get_web().get_currentUser();
context.load(currentUser);
context.executeQueryAsync(
function (sender, args) {
//alert(currentUser.get_loginName());
current_user = currentUser.get_loginName();
SP_Group_1();
}
, FailureHander);
}
function SP_Group_1()
{
groupCollection = context.get_web().get_siteGroups();
pertinent_group = groupCollection.getById(3);
var users = pertinent_group.get_users();
context.load(users);
context.executeQueryAsync(
function (sender, args) {
var userEnumerator = users.getEnumerator();
var i;
while (userEnumerator.moveNext())
{
i = userEnumerator.get_current();
//alert(i.get_loginName());
if(i.get_loginName() == current_user)
{
isUserInGroups = true;
//alert('User In The Group');
break;
}
}
}
, FailureHander);
}
function FailureHander(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}