1

I would like to check in the item displaytemplate if the current user has contribute permissions on the current item. The current item is an site object because in my search query I use contentclass:"STS_Site". How to do that?

My target is to show the results where the current user has contribute permissions, in an other way. like a red border or something. Is this also possible with promoted results?

3
  • How do you want to check? using javascript? Commented Sep 30, 2015 at 8:20
  • What action you want to perform if a user has contribute permissions on an item?
    – Asad Refai
    Commented Sep 30, 2015 at 9:03
  • I would like to show the item in an other way. Add some extra css styling like a red border or something.
    – Ola
    Commented Sep 30, 2015 at 10:53

2 Answers 2

0

Check it using javascript. Make a GET request to the following URL

/_api/web/CurrentUser?$expand=groups

Above URL will return you current user's details along with user groups. From user's groups, you can check easily user has contribute permission or not.

0

You can try the below code in your Item Display template.I would highly recommend to think of an alternative , because every item will make a call to SharePoint, so it would take few more seconds to check the permission.

   function CheckPermission(url) {
        var isContributor = false;
        var clientContext = new SP.ClientContext(url);
        var currentSite = clientContext.get_web();
        clientContext.load(currentSite);
        clientContext.executeQueryAsync(function(){
            var basePerm = new SP.BasePermissions();
            basePerm.set(SP.PermissionKind.viewListItems);
            basePerm.set(SP.PermissionKind.addListItems);
            basePerm.set(SP.PermissionKind.editListItems);
            basePerm.set(SP.PermissionKind.deleteListItems);
            isContributor = web.doesUserHavePermissions(basePerm);
            if(isContributor)
            {
                //user has contribute permission
            }
            else{
                //user does not have contribute permission
            }
        }, function (sender, args) {
            console.log(args);
        });
    }
2
  • If got an exception "Object doesn't support this action" on this row: var clientContext = new SP.ClientContext(url);. Do you now why? I add this javascript inside the item displaytemplate
    – Ola
    Commented Sep 30, 2015 at 9:42
  • Make sure you pass the correct url to the method. Also SP.js is loaded before the call to this method
    – Unnie
    Commented Sep 30, 2015 at 9:44

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.