SharePoint: How do I get the groups for the current user in Javascript or JQuery?
My current project requires me to identify the persona of the logged in user based on their membership of specific SharePoint groups. The first port of call is to extract all the groups for the current user and then work out what they can do.
Sounds easy enough.
I tried the following:
function getUserGroups(success, failure)
{
var d = $.Deferred();
var ctx = SP.ClientContext.get_current();
var collGroup = ctx.get_web().get_siteGroups();
ctx.load(collGroup);
ctx.load(collGroup, 'Include(Users)');
var currentUser = ctx.get_web().get_currentUser();
ctx.load(currentUser);
var o = { d: d, collGroup: collGroup, currentUser: currentUser };
function onQuerySucceeded() {
this.d.resolve(this.collGroup);
this.d.resolve(this.currentUser);
var data = {
"collGroup": this.collGroup,
"currentUser": this.currentUser
};
//return success(this.collGroup);
return success(data);
}
function onQueryFailed(sender, args) {
console.log('Request failed in getUserGroups. ' + args.get_message() + '\n' + args.get_stackTrace());
return failure();
};
ctx.executeQueryAsync(Function.createDelegate(o, onQuerySucceeded), Function.createDelegate(o, onQueryFailed));
}
but I got a permission error in 'ctx.load(collGroup, 'Include(Users)');'
The SharePoint groups were all set to 'Everyone can view membership', so I was stumped. I looked for a new solution to the problem and eventually settled on using SP.Services to get data:
var currentUser = 'ME' // Change this appropriately
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: currentUser,
async: false,
completefunc: function(xData, Status) {
$(xData.responseXML).find("Group").each(function() {
// The Group ID is $(this).attr("ID")
// The Group Name is $(this).attr("Name"));
});
Sounds easy enough.
I tried the following:
function getUserGroups(success, failure)
{
var d = $.Deferred();
var ctx = SP.ClientContext.get_current();
var collGroup = ctx.get_web().get_siteGroups();
ctx.load(collGroup);
ctx.load(collGroup, 'Include(Users)');
var currentUser = ctx.get_web().get_currentUser();
ctx.load(currentUser);
var o = { d: d, collGroup: collGroup, currentUser: currentUser };
function onQuerySucceeded() {
this.d.resolve(this.collGroup);
this.d.resolve(this.currentUser);
var data = {
"collGroup": this.collGroup,
"currentUser": this.currentUser
};
//return success(this.collGroup);
return success(data);
}
function onQueryFailed(sender, args) {
console.log('Request failed in getUserGroups. ' + args.get_message() + '\n' + args.get_stackTrace());
return failure();
};
ctx.executeQueryAsync(Function.createDelegate(o, onQuerySucceeded), Function.createDelegate(o, onQueryFailed));
}
but I got a permission error in 'ctx.load(collGroup, 'Include(Users)');'
The SharePoint groups were all set to 'Everyone can view membership', so I was stumped. I looked for a new solution to the problem and eventually settled on using SP.Services to get data:
var currentUser = 'ME' // Change this appropriately
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: currentUser,
async: false,
completefunc: function(xData, Status) {
$(xData.responseXML).find("Group").each(function() {
// The Group ID is $(this).attr("ID")
// The Group Name is $(this).attr("Name"));
});
Comments
Post a Comment