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 + "&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.