I have a custom action that look like this :

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="MyCustomAction" Location="CommandUI.Ribbon">
        <CommandUIHandler Command="MyCommand"
          CommandAction="javascript:alert('Hello World!');" />

How can I move the alert('Hello World!'); JavaScript code from the custom action definition to a dedicated MyScript.js file ? (in the _layout folder or any other place)


I think you can wrap it in function declared in you file:

function MyFunction()
    alert('Hello World!');

And then call it in the same way:


As for JS file you can add it to Layouts folder and create a delegate control to render link to this file in the page. You can deploy such delegate control in the same feature as your Ribbon customization.


To use the dedicated js file add another customaction with Location=ScriptLink in the xml file, like:

 <CustomAction Id="ITIdea.Ribbon.ListItem.Actions.TaskCompletedButton.Script" Location="ScriptLink" ScriptSrc ="/_layouts/ITIdea/ScriptSource.js"/>

In the script file define your own functions and use these in the CommandAction like Alex showed already.

  • Be aware that if your custom action registered by feature, it would load ScriptSource.js in ALL site pages, even admin ones. – dbardakov May 6 '14 at 10:38

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.