SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom action that look like this :

<Elements xmlns="">
  <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)

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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