Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top