1

How to fully defined and add a user custom action using PowerShell. I was able to define and add a custom action but I don't know how to point to a JS file that contains the JavaScript I would like to execute.

So far, I am there:

$web = Get-SPWeb $web;
$list = $web.lists[$listTitle];
$UCACollection = $list.userCustomActions;
$newUCA = $UCACollection.add();
$newUCA.CommandUIExtension = '<CommandUIExtension xmlns="http://schemas.microsoft.com/sharepoint/">' +
                    '<CommandUIDefinitions>' +
                        '<CommandUIDefinition Location="Ribbon.List.Actions.Controls._children">'+
                            '<Button Id="Ribbon.Documents.UCAID" '+
                                    'Command="Notify" '+
                                    'Sequence="0" '+
                                    'Image16by16="/mysite/siteAssets/my-icon-16x16.png" '+
                                    'Image32by32="/mysite/siteAssets/my-icon-32x32.png" '+
                                    'Description="My description." '+
                                    'LabelText="TitleofcustomAction" '+
                                    'TemplateAlias="o1"/>' +
                        '</CommandUIDefinition>'+
                    '</CommandUIDefinitions>'+
                    '<CommandUIHandlers>'+
                        '<CommandUIHandler Command="Notify" '+
                            'CommandAction="javascript:JsFunctionInJsFileInSiteAssets();" />'+
                    '</CommandUIHandlers>'+
                  '</CommandUIExtension>';
$newUCA.Location = 'CommandUI.Ribbon.ListView';
$newUCA.Title = "MyTitle";
$newUCA.ScriptSrc = "/mysite/siteAssets/function.js";
$newUCA.update();

When using this script I receive :

Validation error has occurred. The property ScriptSrc can be used only if the property Location is set to "ScriptLink".

So, I do put ScriptLink inside, and then I receive :

Validation error has occurred. The property ScriptSrc can be used only if the property Location is set to "!ScriptLink".

What do I have to do to defined a complete and working list ribbon user custom action using PowerShell? When googling, I didn't find even a single example.

0

I have my custom action written JavaScript as follows.

var action = userCustomActions.add();
action.set_location("ScriptLink");
action.set_title(srcurl);
action.set_scriptSrc("~sitecollection/SiteAssets/" + srcurl);
action.set_sequence(srcsequence);
action.update();

According to this I am thinking in your case the following statement can be updated as follows

$newUCA.Location = "ScriptLink"
  • Thank you for the quick reply. Unfortunetely, As I said, puting "ScriptLink" inside $newUCA.Location is giving me the error I described bellow. – ameliapond May 29 '17 at 15:41
0

The ScriptSrc attribute is only applicable when the Location attribute is set to ScriptLink.

What you need to do is to remove the ScriptSrc attribute from your custom action, and then create an additional custom action that serves the script. See below:

<CustomAction Id="ScriptSource1"
              ScriptSrc="/mysite/siteAssets/function.js"
              Location="ScriptLink"
              Sequence="100">
</CustomAction>

If you do not want to have 2 custom actions due to performance concerns, then you need to write JS code that loads the JS file and then executes your function. That JS code would have to be placed inside CommandAction attribute like this:

CommandAction="javascript: <JS code that loads JS file from Site Assets>;
                           JsFunctionInJsFileInSiteAssets();"

You may want to use dynamic loading, namely the SP.SOD.registerSod and SP.SOD.executeFunc functions. See third link in the references below.

Or if the code in the JS file is not too large, you could get rid of the JS file and place your entire code inside the CommandAction attribute.


References:

  • Thank you for your reply. Now I understand why I received this error message. I was actually missing the second custom action. However, haven't check if your solution is working. I will check it and mark your answer as a solution as soon as I'm done. Moreover, I found another solution that I will, as well, describe later. – ameliapond May 30 '17 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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