ARTICLES

Home  > Articles  >  Adding custom context menu items to out of the box SharePoint lists and libraries
Adding custom context menu items to out of the box SharePoint lists and libraries
by Yaroslav Pentsarskyy

Most of the functionality available for users to work with when it comes to SharePoint lists and document library items comes in item context menu. If you’d like to add you own functionality to handle list and document library items you might want to take the same approach and make your custom functions available through item context menus where applicable. In this article we’ll take a look how you can add your own items to existing lists and document libraries context menu.


Context menus in SharePoint are deployed as features to the site collection or to the particular site. The feature can be attached to specific document library or list template, which both are also considered features. Here are steps required to create your own context menu option:

  1. Pick a list or a library you’d like to add your custom context menu item to
  2. Create and install the feature representing a custom context menu item
  3. Create a page that will handle menu action once your custom context menu item was clicked on

Let’s start with addressing the first item. Every time you click Site Actions -> Create and get a list of various document library and list templates – each one of those represent a feature of the specific type. If you like your context menu item to be added to one of the out of the box list or document library item context menu – you need to take down the feature ID of that type of list. You will need that feature type ID further as we go along. To quickly find out the feature ID click Site Actions -> Create and click on one of the items (a document library for example). Once you see the page offering you to create a new library, you need to write down the value of “ListTemplate” in the query string.

Having the ListTemplate value, we can create own item context menu item feature and attach it to that particular list template we took down earlier. Not always you may want make your custom item context menu item available in every single document library. For example, you may have a document library handling specific documents and your custom menu option will handle only those documents. If this is your scenario – check out the follow up article on “Adding custom context menu items to custom SharePoint lists and libraries”.

Here are the steps to create your own custom context menu item attached to our document library:

  1. Navigate to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES” and create a folder with your feature name
  2. Create “Feature.xml” which will define your feature with the content below 

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
                Id="{C7B4AEEE-88D8-46eb-80AB-666228D19280}"
                Scope="Web"
                Title="Open as Word 2003"
                Version="1.0.0.0"
                Description="Displays Open as Word 2003 link in context menu of a document library.">
                <ElementManifests>
                                <ElementManifest Location="MenuItems.xml"/>
                </ElementManifests>
</Feature>

 

    3. Create “MenuItems.xml” which can be named anything else as long as you provide appropriate reference in “ElementManifest” section described above. Here is what we’ll have in our “MenuItems.xml”:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
                <CustomAction
                                Id="{843AEE73-B596-42b8-83FD-104C771E658B}"
                                RegistrationType="List"
                                ShowInLists = "True"
                                RegistrationId="101"
                                Location="EditControlBlock"
                                Sequence="101"
                                Title="Open as Word 2003">
                                <UrlAction Url="~site/_layouts/CustomAction.aspx?ID={ItemId}&amp;List={ListId}"/>
                </CustomAction>
</Elements>


4. The key item in the definition above is to set the “RegistrationId” to the “ListTemplate” value we have taken down earlier. This will ensure the context menu item will be attached only to the types we have specified.

Now that your feature is ready, you need to install and activate it using the following:

Stsadm –o installfeature –name [feature name]

Stsadm –o activatefeature –name [feature name] –url [site url]

Once you have completed the activation successfully, the item context menu item will be added to all of the existing and new document libraries or lists.


Final step is to create an ASPX page, in our case “CustomAction.aspx” that will be called once the user click on your menu item. This page will reside in you “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS”. This part is completely up to your imagination, since you can call web services, access DB, local SharePoint and non-SharePoint objects and more to execute your custom functionality.

Feel free to contact me on my blog in case you have more questions.

Yaroslav Pentsarskyy

www.sharemuch.com