Code, Code, Revolution!
Embedding javascript files into the dll is a good technique I use to make deployment of components easier. When creating properties or other controls that require javascript to work it’s much easier to write the code in a javscript file than to write it inline in the code as a string and then render the string as a script block. It makes sense that the scriptcode is included in the dll which makes deployment much easier. The resource is served to the user through the axd-handler. Here’s a quick guide to embedding your script files into your dll.
This is my project setup.

Edit the properties of your javascript file. In my case “ICallbackJsonProperty.js”. Change the Build Action to “Embedded Resource”.

Check your projects default namespace. In my case it’s MightyLittle.

Open your AssemblyInfo.cs file in the Properties folder of your project. Here you need to define your javascript file as a WebResource in order to serve it through the axd handler. This is my resource.
[assembly: WebResource("MightyLittle.CMSR2.Properties.ICallbackJsonProperty.js", "text/javascript")]
Important to notice the path. Because the default namespace is “MightyLittle” the path must use this prefix. The rest of the path is simply the folder structure to the file in the project. In my project the path to ICallbackJsonProperty.js is CMSR2\Properties\ICallbackJsonProperty.js. Just substitute the backslash with a dot. The second argument is the content type for the resource, in this case text/javascript.
To add the script to a page use the following code:
if (Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "ICallbackJsonPropertyControl") == false) { Page.ClientScript.RegisterClientScriptInclude("ICallbackJsonPropertyControl", Page.ClientScript.GetWebResourceUrl(this.GetType(), "MightyLittle.CMSR2.Properties.ICallbackJsonProperty.js")); }
The path to the javscriptfile is exactly the same as you defined in AssemblyInfo.cs.
The most common problem if your script isn’t being served by the axd-handler is that you messed up the path to your javascript in Assemblyinfo.cs. Another common “doh”-moment is that you’ve set everything up and double checked the paths but still forgot to change the “Build Action” to embed the resource.
A tip is to embedd the javascript file once you are done with your development. Otherwise you’ll be recompiling each time you want to make a change in your script.
With this blog I try to provide useful tips and solutions for programming .NET, Objective-C and more. My name is Björn Sållarp, and I love writing code.
EPiServer property using ajax - ICallbackEventHandler | blog.sallarp.com
April 12th, 2009 at 2:10 pm
[...] my property. First i register my javascript file containing the client script parts of my property, more information on embedding javascript into a dll. Page.ClientScript.GetCallbackEventReference returns the reference to our server method. Note that [...]
vijayan
May 22nd, 2009 at 12:44 pm
Thanks for the handy page. Adding the WebResource reference in AssemblyInfo.cs was a key tip!
I could directly reference the resource (as opposed to including a link to the script URL):
Page.ClientScript.RegisterClientScriptResource(type, resourceName).
/v