Sunday, January 16, 2011

How To add custom menu item in SharePoint ECB Menu (Edit Control Block) via Feature


You can add a custom menu item for an entry control block to a list item or a document in Microsoft Windows SharePoint Services 3.0 by creating a Feature with a CustomAction element. In this way, you can add custom commands to the default SharePoint user interface. These menu commands allow users to perform custom operations on items and documents. For example, you can create a custom menu item for an entry control block for a list item or document that redirects the user to a custom application page. So, lets get started.

First create a new project in VS 2010.
image

Next select, Empty SharePoint Project and give it a proper name
image

In the next screen specify the Site and Security Level for Debugging. Here we have selected to deploy our solution as a farm solution.
image

Now, Select the ‘Feature’ option available in the Solution Explorer, right click onto it and add a new feature
image

If you want, you can rename the Feature folder to another name
image

image

Next define the Scope of your feature. In our case I would like to have this feature to one specific site, so I am selecting the scope as ‘Web’.
image

Now select the project file and right click onto it and select add a new item.
image

Select ‘Empty Element’ template available under SP 2010 category, give it a proper name and select Add.
image

Now, open the newly added Element.xml and replace its content with the content given below.
image

Code Snippet
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <CustomAction
  4.     Id="CA_WF_Init"
  5.     RegistrationType="List"
  6.     RegistrationId="101"
  7.     ImageUrl="/_layouts/1033/Images/RTEDCELL.GIF"
  8.     Location="EditControlBlock"
  9.     Sequence="301"
  10.     Title="My Custom ECB Menu Item" >
  11.       <UrlAction
  12.         Url="~site/default.aspx?ItemId={ItemId}&amp;ListId={ListId}"/>
  13.         <!--Url="<Custom Application Page URL>?ItemId={ItemId}&amp;ListId={ListId}"/>-->
  14.   </CustomAction>
  15. </Elements>

Save it and Select F5. The VS 2010 will deploy your solution and then it will open up the debugging sites default home page. Go to any of the Document Library and select any record, open its ECB Menu and Here comes your custom ECB Menu Item:
image

If you select this custom menu item, you will be redirected to the url you have specified in your Element.XML file. So, very easily you can redirect user to your applications custom page also!

Here certain things you need to know. In SP, everything has some ID associated with it. So, the registration ID specifies in which categories Edit Control Block, you want your custom menu to appear. The complete list of Registration ID in respect to SP 2010 can be found from Mike Smith’s blog post.

If you want to know about the different attributes present in the Custom Action Element, select the following url:
CustomAction Element in depth from MSDN.

Now, here comes another very interesting tricks: Sometimes in real life project the requirement will be to add Custom ECB Menu Item to SPecific List/ Libraries. How to do that? The answer is very simple, whenever you create any list/ library through feature, you provide some unique Template ID in that xml. Copy that ID and place it as your Registration ID. Bang! You are done.

Cheers,
Avik

2 comments:

  1. You can also link to a specific list by specifying the GUID of the list in the registration ID.

    ReplyDelete
  2. I tried doing the same thing but it didn't work for me,I then replaced the RegistrationId by the GUID of the list, still not working, i then replaced the registration type this way :
    RegistrationType="ContentType"
    RegistrationId="{ContentTypeGUID}"

    that was perfect, thank you for your help.

    ReplyDelete