4

Lets say I have a field called locked, which is boolean on a document library.

And I want to make a workflow to Lock or Unlock an item. The Lock button should be enable only if its unlocked and the unlock if its locked.

The lock button will be a custom button on the ribbon for that document library only.

I need to know how to enable/disable this new button on the ribbon based on the value of a selected list item.

1

Here's the easiest, simplest way I could come up with...

if (someCondition == true)
{
   $("#Ribbon\\.ListForm\\.Edit\\.Commit\\.Publish-Large").on("click",function(e){
     e.preventDefault();
   }).addClass("ms-cui-disabled");
}
7
+50

You can use SP.ListOperation.Selection.getSelectedItems() to get the currently selected item.

Here is a code sample:

var context = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
var item;

for (i in selectedItems)    
{
   alert(selectedItems[i].id);
}

Once you have the id of the item, you will have to make a JSOM call to the document library to get the value of the "Locked" field. Once you have the correct value (either locked or unlocked), you can use the EnabledScript property of your custom ribbon button to either enable or disable it.

http://msdn.microsoft.com/en-us//library/ff458385.aspx

<CommandUIHandler
          EnabledScript="javascript:
               function getStatus()
               {
                 //Make JSOM call to get status from list and return true or false.
               }
               getStatus();"/>
</CommandUIHandlers>

Here is a working example of something similar to what you are trying to do: http://henrikfromsweden.blogspot.in/2010/11/code-example-for-enabledscript-elemnt.html

  • 1
    Here you can find a more advanced example using the ribbon and a Page Component blog.mastykarz.nl/… – Remko Oct 31 '13 at 9:41
2

I thought of giving it a try and came up with the below solution. Consider the name of the Boolean Field in the Library/List is BoolField. The following JSOM code will Enable/Disable the Ribbon Button based on the field value. Place the code in a JS file and refer it from ScriptSrc in CustomAction

function EnableDemoButton() {
    var context = SP.ClientContext.get_current();
    var list;    
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);    
    if (CountDictionary(selectedItems) == 1) {
        var web = context.get_web();
        context.load(web);
        var listId = SP.ListOperation.Selection.getSelectedList();
        list = web.get_lists().getById(listId);
        var itemId = selectedItems[0].id;
        if (this.currentItem != null) {
            if (this.itemIdToCheck != null) {
                if (itemId != itemIdToCheck) {
                    GetItemDetails();
                } 
            }                                  
        }
        else {
            GetItemDetails();
        }    
        return this._boolValue; 
    }
    else
    {
        return false;
    }

    function GetItemDetails() {
        this.currentItem = list.getItemById(selectedItems[0].id);
        context.load(this.currentItem);
        context.executeQueryAsync(Function.createDelegate(this, onQuerySuccess), Function.createDelegate(this, onQueryFailed));
    }                
    function onQuerySuccess(sender, args) {

        this._boolValue = this.currentItem.get_item('BoolField');
        this.itemIdToCheck = this.currentItem.get_id();
        RefreshCommandUI();                   
    }
    function onQueryFailed(sender, args)
    {
        alert(args.get_message());
    }
}

The EnableDemoButton will need to be called from EnabledScript. The sample CustomAction is as follows:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="Ribbon.Documents.Manage.Demo"
  RegistrationId="101"
  Location="CommandUI.Ribbon"
  RegistrationType="List">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button Id="Ribbon.Documents.Manage.DemoApp"
          Alt="Demo Application"
          Command="DemoApplication"
          LabelText="Demo Application"
          Image16by16="/_layouts/images/placeholder32x32.png"
          Image32by32="/_layouts/images/placeholder32x32.png"
          TemplateAlias="o1"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="DemoApplication" CommandAction="javascript:
          alert('Hello');
        " EnabledScript="javascript:EnableDemoButton();">
        </CommandUIHandler>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction Id="ExportVersionHistory.Script"
                  Location="ScriptLink"
                  ScriptSrc="/_Layouts/CustomAction/CustomAction.js" />
</Elements>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.