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” because you had “_FIN” appened to the name.

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
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s