Add Custom context menu by Javascript in SharePoint

If you want to add a custom menu item in Document Library’s item context menu just by Javascript without going into the details of  adding any new feature or writing your own assembly then here is a quick trick:

  1. Add a new Content Editor Web Part on your target page
  2. Open the tool pane of the web part
  3. Click Source Editor and add following Javascript code in it
    <script language="javascript">
    function Custom_AddDocLibMenuItems(m, ctx)
    {
      var strDisplayText = "Say Hello World!";
      var strAction = "alert('Hello World')";
      var strImagePath = "";
    
      // Add our new menu item
      CAMOpt(m, strDisplayText, strAction, strImagePath);
    
      // add a separator to the menu
      CAMSep(m);
    
      // false means that the standard menu items should also be rendered
      return false;
    }
    </script>
    
  4. Click Save
  5. Click OK in toolpane to close it
  6. Now open the context menu of an item of the document library and you will see your new menu item in it similar to following

javascript-custom-menu

Please note that this menu item will only be visible on your target not on every page of your document libraries.

Share

2 thoughts on “Add Custom context menu by Javascript in SharePoint”

Comments are closed.

Share via
Copy link
Powered by Social Snap