locked
Custom ribbon button RRS feed

  • Question

  • I want to add a custom ribbon button that would only appear on one list (e.g. in the Actions section) after selecting a single item.
    I want this button to redirect to another page with selected item ID passed in a query string.

    Below is my xml so far (it doesn't seem to work at all - no button is added to the ribbon for that list).
    What's wrong/missing?
      <CustomAction
       Id="TestActionRibbon"
       RegistrationId="{ac4ecd53-8f33-4e0e-b5ab-0236348951c0}"
       RegistrationType="List"
       Location="CommandUI.Ribbon"
       Title="Test Action">
        <CommandUIExtension>
          <CommandUIDefinitions>
            <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
              <Button
                Id="Ribbon.ListItem.New.TestActionButton"
                Alt="Test Action"
                Command="TestActionButton_Clicked"
                LabelText="Test Action"
                Image16by16="/_layouts/images/ERROR.GIF"
                Image32by32="/_layouts/images/ERROR.GIF"
                TemplateAlias="o1"
                />
            </CommandUIDefinition>
          </CommandUIDefinitions>
          <CommandUIHandlers>
            <CommandUIHandler
              Command="TestActionButton_Clicked"
              CommandAction="javascript:alert(selected ID: ' + '{ItemId}');"
              EnabledScript="true" />
          </CommandUIHandlers>
        </CommandUIExtension>
      </CustomAction>
    Wednesday, February 10, 2010 10:53 AM

Answers

  • Hi tsw_milk,

    I spoke with a couple of my colleagues, and they provided the following information with regards to your problem:
    1. Create a feature file with scope=web
    2. Point the feature’s elementmanifest file to a custom file containing your xml
    3. In the elementmanifest file, put <Elements xmlns=”http://schemas.microsoft.com/sharepoint/”> before the definition of the custom action and end the ‘Elements’ tag at the end of the file
    4. Next fix up the CommandAction javascript which is illformatted to be something like : javascript:alert('selected ID:  + {ItemId}');
    5. Now open up SQL Server and look for the id of the list to which you want to add the custom action to. You will find ids in the AllLists table under the tp_ID column
    6. Now install/activate the feature and call IISReset
    Using these steps and your XML above (with the mentioned tweaks), we were able to get your desired functionality working.

    However, you might be disappointed to know that there are some gotchas here.
    1)    We don’t resolve ‘ItemId’ for filtered custom actions on list views. We will only resolve them on the forms/ECB. You could use the {SelectedItemId} token instead.
    2)    Secondly, you need to have javascript in EnabledScript attribute to return true/false. The way you are doing it, it will always return true and the custom action will never be security trimmed.

    Hope this helps.
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Thursday, February 18, 2010 7:29 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Thursday, February 18, 2010 7:28 PM
  • I should also note that we do not support list-scoped feature custom actions. The supported way to do this is to use SPList.UserCustomActions.Add().
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Thursday, February 18, 2010 9:14 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Thursday, February 18, 2010 9:14 PM
  • You need to clear your browser cache after you make changes to the ribbon. That's probably what was holding it up.

    Conveniently enough, we just posted a blog post about making ribbon buttons enabled based on item selection on the SharePoint Team Blog: http://blogs.msdn.com/sharepoint/archive/2010/02/15/enabling-a-button-on-the-ribbon-based-on-selection.aspx

    The ribbon tabs for the view item and edit item pages are not the same as the list view page tab. You'll need to make additional CommandUIDefinitions for those tabs. A list of tabs is available here: http://msdn.microsoft.com/en-us/library/ee537543%28office.14%29.aspx
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Friday, February 19, 2010 8:19 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Friday, February 19, 2010 8:19 PM

All replies

  • Programming custom ribbon buttons was supposed to be easy, doesn't anyone know what's wrong with my code?
    Wednesday, February 17, 2010 4:15 PM
  • Hi tsw_mik,

    I'm customizing the ribbon bar to integrate our Sharepoint 2007 extensions. On which list do you want to extend the ribbon? 

    Try to use the following properties in your customAction element: 

    <CustomAction
    ...
    RegistrationId="101"
    RegistrationType="List" >

     

    Best Regards,

    Tobi

    Thursday, February 18, 2010 6:53 AM
  • I think the RegistrationId (it's the ID of the list I'm trying to add this ribbon button to) and RegistrationType are correct, since I used the same values to add a custom action in the title item menu (with the same functionality). Now I need to add a button to the ribbon to do the same.

    Thursday, February 18, 2010 9:42 AM
  •  

     

     

    I always used the sequence attribute to define the position of the control:

    Sequence
    ="10"

    Thursday, February 18, 2010 10:31 AM
  • Thanks, I tried specifying it too, but it doesn't change anything, so I removed it assuming the control would be added at the end.
    Thursday, February 18, 2010 10:38 AM
  • Hi tsw_milk,

    I spoke with a couple of my colleagues, and they provided the following information with regards to your problem:
    1. Create a feature file with scope=web
    2. Point the feature’s elementmanifest file to a custom file containing your xml
    3. In the elementmanifest file, put <Elements xmlns=”http://schemas.microsoft.com/sharepoint/”> before the definition of the custom action and end the ‘Elements’ tag at the end of the file
    4. Next fix up the CommandAction javascript which is illformatted to be something like : javascript:alert('selected ID:  + {ItemId}');
    5. Now open up SQL Server and look for the id of the list to which you want to add the custom action to. You will find ids in the AllLists table under the tp_ID column
    6. Now install/activate the feature and call IISReset
    Using these steps and your XML above (with the mentioned tweaks), we were able to get your desired functionality working.

    However, you might be disappointed to know that there are some gotchas here.
    1)    We don’t resolve ‘ItemId’ for filtered custom actions on list views. We will only resolve them on the forms/ECB. You could use the {SelectedItemId} token instead.
    2)    Secondly, you need to have javascript in EnabledScript attribute to return true/false. The way you are doing it, it will always return true and the custom action will never be security trimmed.

    Hope this helps.
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Thursday, February 18, 2010 7:29 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Thursday, February 18, 2010 7:28 PM
  • I should also note that we do not support list-scoped feature custom actions. The supported way to do this is to use SPList.UserCustomActions.Add().
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Thursday, February 18, 2010 9:14 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Thursday, February 18, 2010 9:14 PM
  • Thanks for help.
    I'm not sure what was wrong, but it seems to be working now (I haven't made any changes in xml as I already had what you suggested). I only restarted IIS, which at the beginning didn't help, but after a while the button suddenly appeared.
    Anyway I'm now trying to do this programatically, as this seems to be more suitable for me.

    Can you tell me one more thing - how can I make this button become disabled when more than one item is selected in the list? (the selected ID is then null).

    Also with this code the button appears only on the ribbon of the list view page. I thought it would also appear on the view item and edit item pages - how can I amend this?
    Friday, February 19, 2010 10:27 AM
  • You need to clear your browser cache after you make changes to the ribbon. That's probably what was holding it up.

    Conveniently enough, we just posted a blog post about making ribbon buttons enabled based on item selection on the SharePoint Team Blog: http://blogs.msdn.com/sharepoint/archive/2010/02/15/enabling-a-button-on-the-ribbon-based-on-selection.aspx

    The ribbon tabs for the view item and edit item pages are not the same as the list view page tab. You'll need to make additional CommandUIDefinitions for those tabs. A list of tabs is available here: http://msdn.microsoft.com/en-us/library/ee537543%28office.14%29.aspx
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    • Proposed as answer by Jonathan Keslin Friday, February 19, 2010 8:19 PM
    • Marked as answer by tsw_mik Tuesday, February 23, 2010 9:23 AM
    Friday, February 19, 2010 8:19 PM
  • Thanks, one last question.
    I added a custom button to the ListForm.Display and ListForm.Edit and now it appears when viewing and editing the item, but also when creating a new one, which is not quite right. Is there a way of hiding it on the new item page ribbon?
    And if not how can I at least disable it? - what JS method or tokens do I need to use?
    Monday, February 22, 2010 10:46 AM
  • Since it's the same ribbon tab, you cannot declaratively remove the button based on which form it is. You can probably just look for "Edit" or "New" in the URL of the form inside your EnabledScript.
    Jonathan Kern :: UX Developer :: SharePoint Foundation
    Monday, February 22, 2010 7:47 PM