Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)

share|improve this question
add comment

2 Answers

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:

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.

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.