Custom user actions are the commands, which are available for the user access at different locations within SharePoint.These custom actions can come up at any location within SharePoint like ribbon view, site settings page, ECB menu etc: It can be an icon/text, which implements a functionality such as navigating to a landing page, starting a Workflow on an item and so on. All the icons, which come up in the list view ribbon are the examples of custom actions. In many scenarios, we might want to add custom business functionality as a custom user action for ease of access. In this article, we will see, how to add a custom user action to the Ribbon menu.

list

We will try to add an extra custom action to the above ribbon menu, using Javascript object model.

Internal Implementation

Let’s see, how we can do the same programmatically, using JSOM.

Output

Console result is given below:

Console Result:

Full Code: The full code for adding the custom user action to the ribbon is shown below:

  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function() {
  4. SP.SOD.executeOrDelayUntilScriptLoaded(AddCustomUserAction, "sp.js");
  5. });
  6. var oListItem;
  7. function AddCustomUserAction() {
  8. //Get the client context and list object
  9. var context = new SP.ClientContext.get_current();
  10. var list = context.get_web().get_lists().getByTitle("Check List");
  11. //Get the custom user action collection and add the user action
  12. var customUserAction = list.get_userCustomActions().add();
  13. //Set the location of the user action
  14. customUserAction.set_location('CommandUI.Ribbon.ListView');
  15. //Add the properties for the custom action
  16. var userActionExtension = '<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' + '<CommandUIDefinitions>' + '<CommandUIDefinition Location="Ribbon.List.CustomizeList.Controls._children">' + '<Button Id="Ribbon.Documents.New.RibbonTest" ' + 'Command="Notify" ' + 'Sequence="0" ' + 'Image16by16="/_layouts/images/NoteBoard_16x16.png" ' + 'Image32by32="/_layouts/images/NoteBoard_32x32.png" ' + 'Description="Shows the ID of the current list." ' + 'LabelText="Show List ID" ' + 'TemplateAlias="o1"/>' + '</CommandUIDefinition>' + '</CommandUIDefinitions>' + '<CommandUIHandlers>' + '<CommandUIHandler Command="Notify" ' + 'CommandAction="javascript:SP.UI.Notify.addNotification(\'ListId={ListId}\');" />' + '</CommandUIHandlers>' + '</CommandUIExtension>';
  17. //Add the command UI extension and update the custom user action
  18. customUserAction.set_commandUIExtension(userActionExtension)
  19. customUserAction.update();
  20. //Load the client context and execute the batch
  21. context.load(list, 'UserCustomActions');
  22. context.executeQueryAsync(function() {
  23. console.log("Custom User Action added successfully to ribbon.");
  24. }, function(sender, args) {
  25. console.log(args.get_message());
  26. });
  27. }
  28. </script>
Let’s see, how we can implement it in SharePoint. Save the scripts, shown above to a text file and upload it to Site Assets library.

SharePoint Implementation

Going back to the list, we can see that the custom action has come up in the Ribbon, as shown below:

ribbon

Click the custom action.

custom action

It shows the list Id, which is what the custom action was defined for?

Summary

Thus, we successfully created and added the custom action to the ribbon, using JavaScript Object Model. This has been tried and tested in both SharePoint 2016 and Office 365.