Share via


Hiding ECB menus selectively based on an item property

I needed to hide context menu item “Edit Document”, for files of a particular type (which SharePoint determines from the file extension). Here's is how I achieved it :

The menu Item “Edit Document” is rendered via AddDocLibMenuItems function in CORE.JS. Possible approaches to achieve this override in a supported way :

Option1 :

a)Create a customCORE.JS

b)Add extra code, explained in next few lines below in AddDocLibMenuItems function

c)Create a custom masterpage and add a scriptlink for customCORE.js

Option2:

Similar to above option, but instead of creating customCORE.js, we can override either the AddDocLibMenuItems or add an implementation of Custom_AddDocLibMenuItems on the masterpage itself in a script tag, after the scriptlink for CORE.js <SharePoint:ScriptLink language="javascript" name="core.js" Defer="true" runat="server" />)

Option3:

If the change is needed only on one page(View Page of one Library), add a CEWP with an implementation of Custom_AddDocLibMenuItems, which is identical to AddDocLibMenuItems from CORE.js + additional check for the file extension talked about below.

We can override the AddDocLibMenuItems function to not to render the “Edit Document” menu, when the file is of a particular file extension.(In below example, I am checking if it’s a .txt file, then skip the part that renders the “Edit Document” menu.)Below highlighted, is the code that I put in the AddDocLibMenuItems in CORE.js

function AddDocLibMenuItems(m, ctx)

{

...

    else

        {

            var strExt = GetAttributeFromItemTable(itemTable, "Ext", "FileType"); 

            if(strExt != "txt")

                  {

                        var objEditor=StsOpenEnsureEx(currentItemOpenControl+".3");

                        if (objEditor !=null )

                              strDisplayText=L_EditInApplication_Text;

                }

        }

                  if (strDisplayText !="")

                  {

                        strAction="editDocumentWithProgID2('"+currentItemFileUrl+"', '"+currentItemProgId+"', '"

+currentItemOpenControl+"', '"+bIsCheckout+"', '"+ctx.HttpRoot+"', '"+currentItemCheckedoutToLocal+"')";

                              strImagePath=ctx.imagesPath+currentItemIcon;

                              menuOption=CAMOpt(m, strDisplayText, strAction, strImagePath, null, 240);

                              menuOption.id="ID_EditIn_"+currentItemAppName;

                  }

                        }

                  }

            }

      }

...

}

HTH & Happy Customizing SharePoint !!

Comments

  • Anonymous
    October 26, 2010
    Hello, does this work with new 2010 interface? I tried to implement this, but I got no changes there :/

  • Anonymous
    December 20, 2010
    I want to hide context menu items only for a particular document library. can you please suggest the steps to be followed.

  • Anonymous
    January 15, 2015
    THX you so MUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUCH dude !