Exposing Addin Functions to Javascript

Addins can expose .NET functions to the browser environment, which can be called directly from JavaScript. In order to do this, first register the assembly's type with KioWare. Add the following code to the Load() function:

 host.ScriptManager.RegisterJavaScriptInterface(this, this.GetType().Name); 

Register Type with KioWare

Next, add your function to the class. It must be a member function (not static) and it must be marked as public. We recommend only returning simple types, such as void, int, decimal, bool, and string. For complex types, we recommend using a JSON library to serialize the object in the addin, and then calling JSON.parse() in the browser to deserialize the object. Here's the sample function we'll use:

 public string doTestStuff(int MyNum, string TestStr) 
 	 return TestStr + " was received along with " + MyNum.ToString(); 

Add Function

Your new function can now be called from the browser using the following JavaScript code:

 <a href="#" onclick="javascript:alert(Class1.doTestStuff(1, 'test'));">Try It</a> 

If your function performs functionality that would be dangerous for any domain/page on the internet to be able to execute, you can require KioWare to make sure the page is listed in the Securty Access List, with higher trust requirements. To require a specific level of trust, decorate your function with a trust level. For example, to require that KioWare has granted the page Full trust, you could add:


Set Function Trust Level