Check if user is in a group in JavaScript

Useful if you want to show/hide buttons or html/controls on a page layout or in masterpage. Details from http://stackoverflow.com/questions/8841985/sharepoint-10-lists-want-to-limit-form-fields-per-user-groups/8847834#8847834

Link jquery 11 and spservices into your masterpage through script links or custom actions

function:

function IsGroupMember(GroupName)
{
    var isGroupMember = false;
    $().SPServices({
        operation: "GetGroupCollectionFromUser",
        userLoginName: $().SPServices.SPGetCurrentUser(),
        async: false,
        completefunc: function(xData, Status) {
            if($(xData.responseXML).find("Group[Name='" + GroupName + "']").length == 1)                
            {
                isGroupMember = true;
            }
        }
    });
    return isGroupMember;
}

Call function

Admin_Group = "Owners";
if (IsGroupMember(Admin_Group))
    $('.div').show;
else
    $('.div').hide();

 

Leave a Reply

Your email address will not be published. Required fields are marked *