0

I have custom action button and need to gray-out it, if user has no 'Manage Permission' rights for listitem. I created function that can say if user has permisiions or not. But now I cannot integrate it with custom action's EnabledScript parameter. here is my pain:

  function userHasEditPermissions(onComplete) {

    var selectedItem = SP.ListOperation.Selection.getSelectedItems()[0];
    var documentId = selectedItem['id'];

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var currentUser = web.get_currentUser();
    var list = web.get_lists().getByTitle('Draft Documents');
    var document = list.getItemById(documentId);

    clientContext.load(list, 'EffectiveBasePermissions');
    clientContext.load(document, 'EffectiveBasePermissions');
    clientContext.load(currentUser);
    clientContext.load(web);

    clientContext.executeQueryAsync(function () {
        if (document.get_effectiveBasePermissions().has(SP.PermissionKind.managePermissions)) {
            console.log('Current user has edit permissions rights');
            onComplete(true);
        } else {
            console.log('Current user does not have edit permissions rights');
            onComplete(false);
        }
    }, function (sender, args) {
        var errorMessage = args.get_message();
        console.log('jsom query  failed ' + errorMessage);

        onComplete(false);
    });
}

var res = SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    if (SP.ListOperation.Selection.getSelectedItems().length != 1) {
        return false;
    }

    // jsom query is async, so use function to catch result in callback
    userHasEditPermissions(function (flag) {
        return flag;
    });
});

return res;

Here is a part of Custom Action code:

            <Controls Id="TEST.CustomActions.Controls">
              <Button Id="TEST.CustomActions.GetButton"
                      Sequence="1"
                      Image32by32="/_layouts/15/TEST/Images/user_mng_map.png"
                      Image16by16="/_layouts/15/TEST/Images/user_mng_map.png"
                      Image16by16Top="-32"
                      Image16by16Left="-32"
                      LabelText="Document Permissions"
                      Description="Manage Document Administrators and Reviewers"
                      TemplateAlias="o1"
                      ToolTipTitle="Document Permissions"
                      ToolTipDescription="Manage Document Administrators and Reviewers"
                      Command="TEST.OpenDocumentPermissionsPopup"/>
            </Controls>
          </Group>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="TEST.OpenDocumentPermissionsPopup"
                          EnabledScript="javascript:

                            // I add it here, but no luck
                            // ...
                            // 
                            "
                          CommandAction="javascript:

                          // here I can add js successfully
                            "/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.