0

I have a custom list and when the user open the list item Display form there is a custom ribbon. This ribbon should be enabled if the current user is a member of a particular Group. Here is the caml query i used

var id = JSRequest.QueryString["ID"];
if (contextKey == "ApproveRequest") {
    var condition = '<View>' + 
                        '<Query>' + 
                            '<Where>' + 
                                '<And>' + 
                                    '<Eq>' + 
                                        '<FieldRef Name=\"ID\"></FieldRef>' + 
                                        '<Value Type=\"Integer\">' + id + '</Value>' + 
                                    '</Eq>' + 
                                    '<Or>' + 
                                        '<Membership Type="SPGroup" ID="447">' + 
                                            '<FieldRef Name=\"Author\"/>' + 
                                        '</Membership>' + 
                                        '<Eq>' + 
                                            '<FieldRef Name= \"Author\"/>' + 
                                            '<Value Type="Integer"><UserID Type="Integer"/></Value>' + 
                                        '</Eq>' + 
                                    '</Or>' + 
                                '</And>' + 
                            '</Where>' + 
                        '</Query>' + 
                    '</View>';
}

but the above Code is not working.

1 Answer 1

1

You can use the code below to check if the current user is a member of a particular group, then show or hide the custom ribbon button.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){   
    isMember("Some Group Name").done(function(){
        if(data.d.results[0] != undefined){
            //show the custom ribbon 
        }else{
            //hide the custom ribbon
        }
    });
});
function isMember(groupName){
    return $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getByName('"+ groupName +"')/Users?$filter=Id eq " + _spPageContextInfo.userId,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose"}
    });
}
</script>

Or use the JSOM code to achieve it.

<script type="text/javascript">
//Usage
function IsCurrentUserWithContributePerms() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        // The current user is in the [Members] group
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserWithContributePerms, 'SP.js');
function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

        var context = new SP.ClientContext.get_current();
        var currentWeb = context.get_web();

        var currentUser = context.get_web().get_currentUser();
        context.load(currentUser);

        var allGroups = currentWeb.get_siteGroups();
        context.load(allGroups);

        var group = allGroups.getByName(groupName);
        context.load(group);

        var groupUsers = group.get_users();
        context.load(groupUsers);

        context.executeQueryAsync(
                function(sender, args) {
                   var userInGroup = IsUserInGroup(currentUser,group);         
                   OnComplete(userInGroup);
                },
                function OnFailure(sender, args) {
                   OnComplete(false);
                }
        );

        function IsUserInGroup(user,group)
        {
            var groupUsers = group.get_users();
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == user.get_id()) {
                    userInGroup = true;
                    break;
                }
            }
            return userInGroup;
        }
}
</script>

Or the article below for your reference.

Enable\Disable Ribbon button by Users Group SharePoint

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.