3

Is it possible to add some kind of JavaScript validation to ECB items like Edit Item, Check Out and Workflows? What I want is a simple JavaScript alert and redirect to the list if the clicked item does not pass the validation, on the other hand the user will be redirected to the specific page if the item passed the validation (ie Workflow.aspx if the user clicked on Workflows). Overriding functions like AddCheckinCheckoutMenuItem from core.js in an content editor webpart didn't work for me (is it even possible?). I also dont't want to create a whole new ECB, that's why I don't want to use Custom_AddDocLibMenuItems and return false.

What I want in pseudo-code in the ECB onclick for non-custom items:
if(condition) JS alert; redirect back to list; else normal behavior;

2

I've done this before, and worked for me. There are several functions that govern the ECB menu and that you can override (where "can" means you have the ability to do that because this is javascript, but obviously this is not something officially supported, meaning that a future SharePoint update or service pack might break your code).

They are:

AddDocLibMenuItems //(the main function that shows all the ECB actions by calling the other functions listed below)
CAMOpt //(creates all the menu items)    
AddSharedNamespaceMenuItems //(show View/Edit)
AddSendSubMenu //(send to)   
AddDocTransformSubMenu //(convert document)    
AddWorkflowsMenuItem //(create the workflow menu item)
CIMOpt //(custom actions)

Here's an example of an overridden CAMOpt function that shows an alert when the Workflows menu item is clicked:

CAMOpt = function (p,wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc)
{ULSsa6:;
if (wzText == "Workflows") wzAct = "alert('hi there!')";    
var mo=CMOpt(wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc);
    if(!mo)return null;
    AChld(p,mo);
    return mo;
} 
  • 1
    Are you sure it works with a content editor webpart or did you edit the core.js? I just pasted your code in an ce-webpart and no alert came up when clicking on Workflows. It just redirected me to the correct Workflow.aspx – Razze Feb 26 '14 at 11:39
  • don't use a CEWP when you need to include jaavscript code, you can never be sure your code isn't stripped out. Try the code in the console, after the page has loaded, and you will see it working – MdMazzotti Feb 26 '14 at 11:50
  • 2
    Moreover, you need to make sure to call your override after the core.js has loaded. I usually wrap all my code in a function, called with the ExecuteOrDelayUntilScriptLoaded function – MdMazzotti Feb 26 '14 at 11:52
  • ExecuteOrDelayUntilScriptLoaded really did the trick, thanks :) – Razze Feb 26 '14 at 13:12
  • I just did something similar. I took the minified AddDocLibMenuItems function from core.js, copied it into a new js file, made my changes, and then deployed it in a delegate control to the AdditionalPageHead delegate in a feature so that it could be toggled on or off and so that the scope could be controlled. The ExecuteOrDelayUntilScriptLoaded(..., 'core.js'); is the key to ensuring that your hook overrides the OOTB method, just like MdMazzotti said. Good luck. – RJ Cuthbertson Feb 26 '14 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.