0

I recently created a new Sharepoint App to add a custom ribbon action to my ribbon bar.

I want to make sure the button is only enabled when at least one selection to a document selection is made. Disabled otherwise.

I followed a couple of other answers about adding this logic, but nothing is seeming to work... it is always enabled.

This is my code..

<CommandUIHandlers>
        <CommandUIHandler Command="Invoke_RibbonCustomAction1ButtonRequest"
                          EnabledScript="javascript: function enableCopyLinksButton() {
                                        console.log('running script');
                                        var context = SP.ClientContext.get_current();
                                        console.log(SP.ListOperation.Selection.getSelectedItems(context).length);
                                        var result = SP.ListOperation.Selection.getSelectedItems(context).length >= 1;
                                        return result;
                                      }
                                      enableCopyLinksButton();"
                          CommandAction="~remoteAppUrl/CustomActionTarget.aspx?{StandardTokens}&amp;SPListItemId={SelectedItemId}&amp;SPListId={SelectedListId}&amp;SPSource={Source}&amp;SPListURLDir={ListUrlDir}&amp;SPAppWebUrl={AppWebUrl}&amp;SPHostWebUrl={HostWebUrl}"/>
  </CommandUIHandlers>

I added some console logging there to even see if anything is fired in the console, but i see nothing. So not even convinced that the script is even firing at all..

Am i approaching this wrong by any chance? (I also tried to add javascript:alert('test'); to my CommandAction, but then the app breaks when I try deploy it. Seems like it doesnt like it anyways)

1 Answer 1

0

In your EnabledScript, you both define the function and call it. Actually, the code right after javascript: must be the code returning true or false. And you cannot defined a function in there.

You have to embed your a JavaScript function enableCopyLinksButton elsewhere and only call it:

EnabledScript="javascript: enableCopyLinksButton();"

You then have to define the function enableCopyLinksButton in the master page, or in a script custom action (see attribute ScriptSrc at https://msdn.microsoft.com/fr-fr/library/office/ms460194.aspx), or in SPWeb.CustomJavaScriptFileUrl (see https://msdn.microsoft.com/fr-fr/library/microsoft.sharepoint.spweb.customjavascriptfileurl.aspx), or by any other mean.

2
  • Thanks Evariste. I came across this other answer - sharepoint.stackexchange.com/questions/132096/… - and there the person says you can use 'inline scripting' when defining and calling the function. This not true anymore? I'm also hoping to keep all code related to the app within the app. Hoping to avoid having the need to add additional code outside of the app package... But if need be, I will have to.. :)
    – Simon
    Jul 20, 2015 at 16:11
  • You probably can keep everything in the app by declaring the script file as a module, and referencing it from ScriptSrc tag in your custom action.
    – Evariste
    Jul 20, 2015 at 16:14

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.