0

How could I hide some menu items from a ECB menu in a Sharepoint add-in, based on permissions? My Sharepoint application is Sharepoint hosted not provider hosted, so the javascript injection method wouldn't work.

Thanks

Rahma
  • 255
  • 3
  • 21
  • 1
    Your application will always run with the current users permissions. If a permission level on a item in your menu has a higher permission level than the current user the user won't be able to access it. – Kodz Nov 14 '15 at 08:52
  • Thank you for your reply. Is it possible to use usergroups and javascript? I can't find a workaround .. – Rahma Nov 14 '15 at 18:32
  • 1
    Sure its possible, the best way i'd say would be to check if the current user is a member of a group. – Kodz Nov 14 '15 at 18:52

2 Answers2

1

Function to check if user is member of specified group

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

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

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

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

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

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

        currentContext.executeQueryAsync(OnSuccess,OnFailure);

        function OnSuccess(sender, args) {
            var userInGroup = false;
            var groupUserEnumerator = groupUsers.getEnumerator();
            while (groupUserEnumerator.moveNext()) {
                var groupUser = groupUserEnumerator.get_current();
                if (groupUser.get_id() == currentUser.get_id()) {
                    userInGroup = true;
                    break;
                }
            }  
            OnComplete(userInGroup);
        }

        function OnFailure(sender, args) {
            OnComplete(false);
        }    
}

usage

function IsCurrentUserHasContribPerms() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        // The current user is in the [Members] group!
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(IsCurrentUserHasContribPerms, 'SP.js');

Source from here

Community
  • 1
  • 1
Kodz
  • 380
  • 2
  • 13
0

thank you for your help. Finally I've got what I wanted to customize in my page. I've overriden the CreateMenuOption in core.js by creating another file (I've used Chrome to get the code). I've used the following js file too : https://spservices.codeplex.com/ in order to get the group of the connected user.

Rahma
  • 255
  • 3
  • 21