I have a sandboxed sharepoint project, in which I have created a module, put jQuery there and referenced it from my webpart's ascx page like this:

<script src="/Content/jquery-1.7.2.min.js" type="text/javascript"></script> 

The thing seems to load properly and the scripts work - the thing is when I'm setting some personalizable properties in the webpart's options, its supposed to fire a jquery script from codebehind, like this:

    protected override void CreateChildControls()
             new LiteralControl("<script language=\"javascript\" type=\"text/javascript\">jQuery(document).ready(setClockOffset($, " + ClockTimeZone9 + ",\"" + ClockTimeZone8 + "\"));</script>"));


And this gives me the following error: Uncaught ReferenceError: jQuery is not defined It seems like LiteralControl adds the markup as the first element inside head, instead of possibly adding it after the jquery reference. What would be the best way to approach an issue like that?

2 Answers 2


In my 365 projects, I created a folder called "Modules" inside the "Modules" folder I created another folder called "Scripts", I then put my Jquery and other scripts in this folder.

You can put the following in your code.

<script> window.jQuery || document.write('<script src="<%= SPContext.Current.Site.Url %>/path_your_module/Scripts/jquery-1.7.1.min.js"><\/script>'); </script>

Or you can create a class what inherits a WebPart and create an override method for OnInit. From there you can create a new literal control:

Controls.AddAt(0, new LiteralControl("<script type=\"text/javascript\"> window.jQuery || document.write('<script src=\"" + WebUrl + "/path_your_module/Scripts/jquery-1.7.1.min.js\"><\\/script>');</script>"));

WebUrl is a property with the url of your site.

Don't forget to inherit it in your webpart within the class you created!


Ok, so the solution to this what seemed to be trivial problem was actually a bit longer than i would've expected.

first I added the jquery sctiptfile to a module "Content", and referenced it inside the webpart's Elements.xml file, like this:


Also all my javascript logic I have moved to a separate file and referenced it the same way. This way I could add whatever I wanted in the form of adding a LiteralControl and it worked. Obviously the javascript had to be a bit modified for this approach to work.

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.