2

I have a custom action that look like this :

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

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)

2 Answers 2

2

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:

CommandAction="javascript:MyFunction();"

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.

2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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