0

I have created a custom action button that appears on the DisplayForm for a specific content type.

On the list view I would also like to enable the button but only when the specific content type is selected and only if there is only one item selected.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Description="Button Blah"
Title="Button Title"
Id="{53801DF2-A778-4E93-8191-ACBC06C7D4DD}"
Location="???????????????"
RegistrationType="ContentType"
RegistrationId="0x010014D754C688D12A4FB4C811B4B4F67A5C"
Sequence="0"
Rights="ViewListItems"
xmlns="http://schemas.microsoft.com/sharepoint/">
<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Define the (UI) button to be used for this custom action -->
  <CommandUIDefinitions>
    <CommandUIDefinition Location="Ribbon.ListForm.Display.Manage.Controls._children">
      <Button Id="{10C2173C-537F-4D3D-8394-9ADB8486CEBE}"
      Command="{99AAAD72-4AB6-4EC0-8B5A-E3F194145A4D}"
      Image32by32="~site/_layouts/Images/GCMS/SomeImage32.png"
      Image16by16="~site/_layouts/Images/GCMS/SomeImage16.png"
      Sequence="0"
      LabelText="Button Label Text"
      Description="Button Blah"
      TemplateAlias="o1" />
    </CommandUIDefinition>
  </CommandUIDefinitions>
  <CommandUIHandlers>
    <!-- Define the action expected on the button click -->
    <CommandUIHandler Command="{99AAAD72-4AB6-4EC0-8B5A-E3F194145A4D}" CommandAction="~site/_layouts/custompage.aspx?List={ListId}&amp;ID={ItemId}" />
  </CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>

I have tried the following in the CommandUIHandler:

<CommandUIHandler
      Command="MYSOLUTION.CreateActionCommand" 
      EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length = 1;"
      CommandAction="javascript: alert('My Command!');" />
  </CommandUIHandlers>

but that enables the button constantly.

If I use the following:

<CommandUIHandler
      Command="MYSOLUTION.CreateActionCommand" 
      EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length > 0;"
      CommandAction="javascript: alert('My Command!');" />
  </CommandUIHandlers>

It works when I select one item or more.

But my requirement is to only enable the button when ONLY one item is selected - not multiple selected items.

How do I enable/disable a button when a specific content type is selected in a list view?

2
  • Does the list contain only one content type? Commented Feb 24, 2015 at 9:10
  • @NadeemYousuf at the moment it does but it may not in future as new content types could be added. Commented Feb 24, 2015 at 9:11

6 Answers 6

6

I think you are almost there except that you have used assignment operator which will always return TRUE.

Instead you have to use comparison '==' or '===' operators.

update in below line

EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length === 1;"
2

If the list contains only one content type then in the CustomAction you need to define following three properties:

RegistrationType="ContentType"
RegistrationId="YourContenttype"
Location="CommandUI.Ribbon.ListView"

In the CommandUIHandlers, there is EnabledScript attribute which helps in enabling and disabling a button. You can refer to an .js file which contains the code for enabling/disabling like:

<CommandUIHandlers>
    <CommandUIHandler
     Command="ExportVersionHistory"
     CommandAction="javascript:ExportVersionHistory();"
     EnabledScript="javascript:ExportVersionHistoryEnable();"/>
</CommandUIHandlers>

Or you can also write inline code like:

EnabledScript="javascript:
        function enableBulkApproveButton() {
        var context = SP.ClientContext.get_current();
        return SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
        }

But if the list contains more than one content types then you need to check the content type of the selected item. Here in this example, the button is enabled if versioning is on in the list. In a similar way, you need to get hold of the selected item and check it content type and compare it with a predefined content type:

function listSuccess(sender, args) {
    this.versioningEnabled = this.list.get_enableVersioning();
    RefreshCommandUI();
}


function listFailed(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}
function EnableDisableButton() {
    var items = SP.ListOperation.Selection.getSelectedItems();
    var ci = CountDictionary(items);
    if (ci > 0) {
    var context = SP.ClientContext.get_current();
    this.list = context.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
    if (this.versioningEnabled === undefined) {
        context.load(this.list);
        context.executeQueryAsync(Function.createDelegate(this, this.listSuccess), Function.createDelegate(this, this.listFailed));
    }
        return this.versioningEnabled;
    }
}

Check the code of my codeplex project. It may be useful: https://exportversionhistorysp2013.codeplex.com/SourceControl/latest#NY.ExportVersionHistory/NY.ExportVersionHistory/ExportCustomAction/Elements.xml

2

I think you're command handler lacks one character. Instead of doing 'length = 1', you should do 'length == 1'. Now you're assigning 1 to length, which always results in a positive result. The > 1 works because that's a compare operator and not an assign one :) I verified and this seems to work.

1

CommandUIHandler has not only Command and CommandAction attributes but also EnabledScript attribute. It may contains javascript function returning true/false. You may use javascript CSOM to get content type of the item(s) and enable/disable your button. Hiding button completely is different story and is not usual in SP.

<CommandUIHandler
          EnabledScript="javascript:
               function getStatus()
               {
                 //get content type of selected items and return true or false.
               }
               getStatus();"/>
</CommandUIHandlers>

You can determine selected item(s) in getStatus this way:

var context = SP.ClientContext.get_current();

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

    for (i in selectedItems)    
    {
       alert(selectedItems[i].id);
    }
4
  • It was actually enable/disable button that I am trying to achieve. If there's more than one item selected I want to disable the button too because my code should only run on one item at a time. I will edit my question. Commented Feb 17, 2015 at 21:42
  • 1
    The idea is still the same. The only difference will be that you check number of items in selectedItems collection. Here is sample of reading selected item details social.msdn.microsoft.com/Forums/sharepoint/en-US/… .
    – Jan Vanek
    Commented Feb 17, 2015 at 22:23
  • I'm a bit confused by the placement of the getStatus() within the EnabledScript tag. It seems I need a separate CommandUIHandler for this function or should I put the EnabledScript within my existing CommandUIHandler beside my CommandAction. Could you provide a fuller example in your answer to help me with this please? Commented Feb 23, 2015 at 16:58
  • No you can use it in the same CommandUIHandler.
    – Jan Vanek
    Commented Feb 24, 2015 at 10:13
1

I used the following and it works to enable/disable the button when only one item is selected.

<CommandUIHandlers>
    <CommandUIHandler
      Command="GCMS.CreateActionCommand"
      EnabledScript="javascript:var EnableDisable = function() {
                      this.clientContext = SP.ClientContext.get_current();
                      this.selectedItems = SP.ListOperation.Selection.getSelectedItems(this.clientContext)
                      var ci = CountDictionary(selectedItems);
                      return (ci == 1);
                      };
                      EnableDisable();"
      CommandAction="javascript:OpenPopUpPageWithTitle('{SiteUrl}/_layouts/MYSOLUTION/mypage.aspx?List={SelectedListId}&amp;ID={SelectedItemId}&amp;Source={Source}', RefreshOnDialogClose, 800, 700,'DO SOMETHING GREAT')" />
  </CommandUIHandlers>
-1

You can use instead of =1 EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length === 1;"

0

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.