Add Custom context menu by Javascript in SharePoint

By Namwar Rizvi - Last updated: Thursday, February 19, 2009 - Save & Share - 2 Comments

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
Posted in Uncategorized • Tags: , Top Of Page

2 Responses to “Add Custom context menu by Javascript in SharePoint”

Comment from Clem
Time July 28, 2014 at 5:57 pm

Have you tried this in SP2010? I tried it but could not get it to work.

Comment from Clem
Time July 28, 2014 at 6:04 pm

What do you use to call this method? It’s not automatically called.

Write a comment