Modifying Default ECB Menu Items In Sharepoint 2007

20 October 2010

Recently, I was tasked with replacing the out-of-the-box Version History menu item, and first of all came across the MSDN article on how to hide a menu item in the ECB. The original plan was to remove the existing item, and replace it with a custom Version History menu item - but the MSDN article recommends making an entire copy of core.js and modifying the copy, which seemed like a lot of overhead for what was essentially a small change.

So instead, I did a bit more research - and came across a fantastic resource for ECB customization, Jan Tielen's three-part series on the matter.

All I wanted to do was redirect the VersionHistory action to a custom Application Page that would be deployed in the _layouts directory, which turned out a lot easier than expected. Having a look at the existing ECB defined in core.js:

function AddListMenuItems(m, ctx)
{
    if (typeof(Custom_AddListMenuItems) !="undefined")        //here is where we can plug-in our own list menu items via Javascript
    {
        if (Custom_AddListMenuItems(m, ctx))
            return;
    }

        ...

    CAMSep(m);

        //if versioning is enabled, add the version history menu item
    if (ctx.verEnabled==1)
    {
        AddVersionsMenuItem(m, ctx, currentItemEscapedFileUrl);
    }

        ...
}

//this sets up the ecb menu item
function AddVersionsMenuItem(m, ctx, url)
{
    if (currentItemID !=null)
    {
       var strCurrentItemID=currentItemID.toString();
       if (strCurrentItemID.indexOf(".0.") >=0 )
       return;
    }
    if (!HasRights(0x0, 0x40))
      return;
    strDisplayText=L_Versions_Text;
    strAction="NavigateToVersionsAspx('"+ctx.HttpRoot+"', 'list="+ctx.listName+"&ID="+currentItemID+"&FileName="+url+"')";            
    strImagePath=ctx.imagesPath+"versions.gif";
    var menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 800);
    menuOption.id="ID_Versions";
}

function NavigateToVersionsAspx(strHttpRoot, strArgs)
{
        //redirect to the version history page
    STSNavigate(strHttpRoot+"/_layouts/Versions.aspx?"+strArgs+"&Source="+GetSource());
}

So we can see how it works out of the box - there's some Javascript to look at the ctx object (which stores contextual data around the current list, etc), and if the conditions are right to add the menu item, it does so through the CAMOpt() method. Thus, the easiest way to redirect the user to a different version history page is to overwrite the NavigateToVersionsAspx function.

//this function is called by core.js if it finds it function CustomAddDocLibMenuItems(m, ctx) { //overwrite the existing definition of NavigateToVersionsAspx NavigateToVersionsAspx = new Function("strHttpRoot", "strArgs", "STSNavigate(strHttpRoot+'/layouts/MySolution/MyCustomActionPage.aspx?'+strArgs+'&Source='+GetSource());"); }

By including the javascript above in your solution (either in your masterpage, or a webpart, or anything, really) you should now find that when users select "Version History" in the ECB, they are redirected to your page, not the default one.

I imagine it would be possible to overwrite AddVersionsMenuItem() as well, but I haven't tested this personally - if you do, let me know if it works!

Tags: SharePoint, ecb, Javascript

Add a Comment

No Comments