Using SPServices “GetGroupCollectionFromUser” operation
SharePoint 2010 Used
We had a task in which we had to show and hide items on a page based on which group a user belonged to.
We needed a way to identify if the current user belonged to a specific group. We were able to accomplish this using the “GetGroupCollectionFromUser” operation of SPServices.
Lets say you had a site with many groups but 5 of them were “Finance”. To identify “Finance”, it was appended with “_FIN”.
Using “GetGroupCollectionFromUser” operation, here’s the function that was created.
A function DecideGroup(url) with one parameter called ‘url’ was created.
see below:
function DecideGroup(url)
{
var url = siteUrl+"/";
var isFIN;
var bsoGrpName=null;
$().SPServices({
operation: "GetGroupCollectionFromUser",
userLoginName: $().SPServices.SPGetCurrentUser(),
async: false,
webURL: url,
completefunc: function(xData, Status) {
$(xData.responseXML).find("Group[Name$='_FIN']").each(function() { //find any group ending in '_FIN'
finGrpName = $(this).attr("Name");
return false; //Once you find they are in a finance group, no need to go continue with the loop
});
}
});
return finGrpName; //return group name
}