3

I am using a Delegate control to add a menu item in SharePoint Edit control block. I want to show the menu item based on two conditions.

  • Versioning of the list should be enabled.
  • Current user should have deleteListItems permissions.

I get the versioning information from the ctx object. However, since there is no way to get the permission information from ctx object, I have written JSOM for it. Following is the code:

<script type="text/javascript">
    var context = null;
    var web = null;
    var hasPermissions = false;
    function Custom_AddListMenuItems(m, ctx) {
        CheckUserPermissions();
        if (hasPermissions) {
            AddECBMenuItems(m, ctx);
        }
    }

    function Custom_AddDocLibMenuItems(m, ctx) {
        CheckUserPermissions();
        if (hasPermissions) {
            AddECBMenuItems(m, ctx);
        }
    }

    function AddECBMenuItems(m, ctx) {
        var pageUrl = ctx.HttpRoot + "/_layouts/NY.ExportVersionHistory/ExportVersionHistory.aspx?ID=" + currentItemID + "&amp;List=" + ctx.listName;
        if (ctx.verEnabled) {
            CAMOpt(m, "Export Version History", "window.open('" + pageUrl + "');", "/_layouts/images/NY.ExportVersionHistory/Excel_Small.png");
            CAMSep(m);
        }
    }

    function CheckUserPermissions() {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        context.load(web, 'EffectiveBasePermissions');
        context.executeQueryAsync(onSuccess, onFailure);
    }
    function onSuccess() {
        if (web.get_effectiveBasePermissions().has(SP.PermissionKind.deleteListItems)) {
            hasPermissions = true;
        }
    }
    function onFailure(sender, args) {
        alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
    }
</script>

Now when I log in with a user having deleteListItems permissions and browse to a list in which versioning is enabled and click ECB of any item, the first time it doesn't show the new ECB item (Export Version History) . It does show on subsequesnt ECB clicks. The problem arises because of the asynchronous behavior of the JSOM since hasPermissions field will be false for the first time. In ribbon we have RefreshCommandUI() to avert this issue. How can I fix this issue in ECB?

UPDATE

I also tried several other options like moving the code which checks UserPermissions to OnSuccess method, Using Promises with the JavaScript Client Object Model etc. Here is a pictorial representation of what happens when I execute the code. The ECB menu is already generated by the time the code in OnSuccess method is executed.enter image description here

enter image description here

2 Answers 2

1

Instead of JSOM based solution I suggest alternative approach: Use SPSecurityTrimmedControl control with properly set PermissionsString

1
  • Thanks for the suggestion. I resolved the particular issue using SPSecurityTrimmedControl. However, I will keep the question open, as there can be situations where we need to use JSOM to get the info from the site, list etc. +1 from my side Commented Dec 12, 2013 at 17:05
0

CORE.js file has a function named HasRights which takes two parameters. Passing EmptyMask value of 0x0 and 0x8 for DeleteListItems fixes this. The code looks like this:

function Custom_AddListMenuItems(m, ctx) {
        AddECBMenuItems(m, ctx);  
    }

    function Custom_AddDocLibMenuItems(m, ctx) {
        AddECBMenuItems(m, ctx);
    }

    function AddECBMenuItems(m, ctx) {
        var pageUrl = ctx.HttpRoot + "/_layouts/NY.ExportVersionHistory/ExportVersionHistory.aspx?ID=" + currentItemID + "&amp;List=" + ctx.listName;
        if (ctx.verEnabled && HasRights(0x0, 0x8)) {
            CAMOpt(m, "Export Version History", "window.open('" + pageUrl + "');", "/_layouts/images/NY.ExportVersionHistory/Excel_Small.png");
            CAMSep(m);
        }
    }

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.