4

I am trying to call JavaScript function into my Custom Ribbon command. I was able to create my Custom Ribbon Action using this answer. This perfectly worked for me in creation of Custom Ribbon Action Button and JavaScript Alert also called successfully.

But my question is: Can we call JavaScript function from Custom Ribbon Action from some JS file uploaded in any library in Host Web of SharePoint App? (App may be SharePoint Hosted/Provider Hosted)

I have gone through this answer which says it's impossible to call JavaScript function but I am able to call JavaScript alert() in Custom Action

When I tried following code in my App Installed event, I got error like

The value specified for property Location for custom action {caa19918-a0b1-4cb9-8200-bafc2df31d43} is invalid. The value must be !ScriptLink.

clientContext.Load(clientContext.Web);
List list = clientContext.Web.Lists.GetByTitle("Details"); // or use GetByID or whatever
UserCustomActionCollection collUCA = list.UserCustomActions;

UserCustomAction Scriptaction1 = collUCA.Add();
Scriptaction1.Sequence = 2;
Scriptaction1.ScriptSrc = "~SiteCollection/SiteAssets/Assets/Scripts.js";
Scriptaction1.Title = title;
Scriptaction1.Location = "ScriptLink";
Scriptaction1.Update();
clientContext.ExecuteQuery();

When I remove ScriptLink attribute and add any other attribute then I get error like

A validation error has occurred. The ScriptSrc property can only be used when the Location property has the value "ScriptLink".

So, I just wanted to know How can reference JavaScript to my Custom Ribbon Action from C# code?

2
  • hey what is "collUCA" in your code
    – Codec
    Jul 23, 2016 at 6:59
  • Sorry, see updated question Jul 23, 2016 at 7:00

1 Answer 1

3

The mistake you are doing is you are using List's UserCustomActions, but here your ScriptLink is the part of SPWeb or SPSite. So you code need to be like

 UserCustomAction Scriptaction1 = clientContext.Web.UserCustomActions.Add();

instead of

UserCustomAction Scriptaction1 = collUCA.Add();

Refer this

1
  • 1
    Hey this worked like charm :-) Jul 23, 2016 at 7:20

Your Answer

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

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