SharePoint: Conditionally show menu item in ECB Menu

Scenario: Show menu item in SharePoint ECB based on a condition e.g when versioning is enabled in the List or Document Library.

Solution: One of the ways of adding items to ECB is to create a delegate control. In the control the code is written in two JavaScript functions Custom_AddListMenuItems and Custom_AddDocLibMenuItems. The SharePoint context object (ctx) provides lots of information like the list template, list name etc. Its verEnabled property specifies whether the versioning is enabled in the list or not. please visit Tom Van Gaever - Blog for a complete list of properties provided by ctx object. 

  1. Create a user control under SharePoint ControlTemplates mapped folder.
  2. Create an elements file and specify the Id as "AdditionalPageHead" and ControlSrc to be the ascx control created in first step as shown:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
       <Control Id="AdditionalPageHead"
           ControlSrc="~/_CONTROLTEMPLATES/NY.ExportVersionHistory/ECBDelegateControl.ascx" />

In the ascx control we need to define Custom_AddListMenuItems(m, ctx) and Custom_AddDocLibMenuItems(m, ctx)

<script type="text/javascript">
    function Custom_AddListMenuItems(m, ctx) {
        AddECBMenuItems(m, ctx); 

    function Custom_AddDocLibMenuItems(m, ctx) {
        AddECBMenuItems(m, ctx);

    function AddECBMenuItems(m, ctx) {
        var pageUrl = ctx.HttpRoot + "/_layouts/NY.ExportVersionHistory/ExportVersionHistory.aspx?ID=" + currentItemID + "&amp;List=" + ctx.listName;
        if (ctx.verEnabled) {
            CAMOpt(m, "Export Version History", "'" + pageUrl + "');", "/_layouts/images/NY.ExportVersionHistory/Excel_Small.png");

The code above first creates a page Url and appends to it the current item ID and the list name. It then check the verEnabled property of ctx object. If condition evaluates to true it adds an a menu item to ECB menu.