Adding custom context menu items to custom SharePoint lists and libraries

In my previous article we looked at how you can attach your own document library or list custom context menu item to any existing document library or list type. This means that any library of that type will have your custom action displayed, which may not necessarily meet your needs in case you want to target libraries of the specific type. In this article we’ll cover how to narrow down the scope of your custom item context menu option.

After all, you don’t want to confuse users with the functionality in other document libraries if that particular function is not applicable to them. In this case we need to create our own document library or list based on the existing template. Although, it may seem complex, all we are going to do is to duplicate existing SharePoint feature and change the template ID and Title of the feature.

Here is how to do that:

1. Navigate to “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”

2. Find the out of the box document library or list template you’re trying to have your document library based on. In our case we’ll use “DocumentLibrary”.

3. Copy the folder of the feature (i.e. “DocumentLibrary”).

4. Open “Feature.xml” in that particular folder and find where is “ElementManifest” Location value is pointing to. In our case it’s “ListTemplates\DocumentLibrary.xml”.

5. Specify your own value for “Title” and “Description”.

6. Open the element definition file in step 4 (i.e. DocumentLibrary.xml) and find replace the value of “Type” to something unique. For example append original value with your own type number.
 

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 [siteurl]

When you navigate to Site Actions -> Create, you will see your feature Title and Description appear on the list along with all of the other out of the box document libraries and lists. From here we can create our document library as any other. In our particular example – there is no difference between ours and the “real” document library, just the type ID and possibly Title has changed.

Our document library type will inherit all of the context menu items that any other document library would have and help us attaching our custom context menu item.

Now, as described in my last article “Adding custom context menu items to out of the box SharePoint lists and libraries” step 2, we’ll create a feature that will attach to that specific document library type.

Here are the steps:

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 ID you have assigned when creating your custom document library or list. This will ensure the context menu item will be attached only to the types we have specified.

After creating your ASPX handler for the custom action and installing and activating the feature you will see a custom menu item attached to your existing and new document libraries.

This entry was posted in MOSS, sharepoint, wss and tagged , . Bookmark the permalink.

Comments are closed.