Please upgrade your web browser now. Internet Explorer 6 is no longer supported.
Thinking Web Solutions?
We create smart, fun, functional websites that make your web a better place.

Overriding Core Javascript Functions in SharePoint

Recently I spent a number of hours banging my head against a wall trying to customize some of the rich DHTML functionality in SharePoint. Basically I wanted to remove the Workflows option from the default list item context menu (Edit Control Block).

WithWorkflow

I couldn't use a feature as this doesn't work for the built in elements and I didn't want to mess with CORE.js as it's never good practive and would effect all doc libs. So I worked out which method to override and then added this code inside PlaceHolderMain for the allitems.aspx page like so:

<script type="text/javascript">

function AddWorkflowsMenuItem(m, ctx)
{
//do nothing
}

</script>

The idea is that it will override the defualt behaviour of adding the menu with nothing - and hence not show the menu. For some reason this didn't work, in fact when debugged, the JavaScript was throwing an error. Interestingly it worked in FireFox so I knew I wasn't doing anything wrong. Initially I thought maybe this could be a browser compat thing but overriding a method is the same in any browser.

Eventually it dawned on me that the problem was the core.js script had not yet been loaded. The culprit was the ScriptLink control:

<SharePoint:ScriptLink language="javascript" name="core.js" Defer="true" runat="server"/>

Basically this control is referencing the CORE.js external javascript file. However becuase the defer tag is specified it is loaded after my function override is declared. This deferred loading is usually a good thing as the page doesn't have to halt it's loading for the script - instead it is loading in the background. This gives a perceived performance boost to the user.

In my case I was able to add the scriptlink without the defer tag to just the page I needed to (allitems.aspx). All other pages in the site can still benefit from the deferred loading and I get the desired functionality:

WithOutWorkflow

7 comments for “Overriding Core Javascript Functions in SharePoint”

  1. Anders Jacobsen  3/19/2009

    Hi Great info. I have also written a simular blog entry a while ago at: http://www.pings.dk/blog/archive/2007/10/15/overriding-javascript-functions-in-sharepoint.aspx My method also allows you to "decoreate" existing Sharepoint functions instead of completly overwrting them. Regards Anders

  2. Sham  6/1/2009

    Are you able to post the full script you used to get this to work?

  3. Zac Smith  6/8/2009

    Hi Sham, all the code that I used is contained in the article.

  4. Fred Kastl  2/25/2010

    There is another way without changing the Defer flag. You can override the function after the core.js is loaded. Example ( with jQuery ):

  5. Fred Kastl  2/26/2010

    $(document).ready(function(){ AddWorkflowsMenuItem = function(m, ctx){//do nothing});

  6. Huzefa Mala  5/7/2010

    Great post! Saved me! Thanks!

  7. Fredzool  4/21/2011

    This can work on doc libs


    <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,All_Documents;" DefaultView="TRUE" MobileView="True" MobileDefaultView="True" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/dlicon.png" Url="Forms/AllItems.aspx">
    <ViewHeader>
    <!--overide core.js-->
    <HTML><![CDATA[<script type="text/javascript" src="/_layouts/1033/CustomCORE.JS" defer="TRUE"></script>]]></HTML>

Post a comment