SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use jQuery in a Sandboxed Visual WebPart (2010):

protected void Page_Load(object sender, EventArgs e)
    string jQuerySrc = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ProjectNS.Scripts.jquery.min.js"); // Correctly assigned in debugger
    // Script tag not present in HTML (regardless of whether the WebResources call would actually succeed or fail.)
    Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "ProjectNS.Scripts.jquery.min.js", jQuerySrc); 

I've also now tried this alternative with no apparent change:

ScriptManager.RegisterClientScriptResource(Page, this.GetType(), "ProjectNS.Scripts.jquery.min.js");

When I deploy and run, this code seems to run fine and generates no errors. Unfortunately, the resulting page's html does not contain the references to the script.

If this is indeed a silent failure due to Sandbox restrictions, I guess I understand. But it could be another oversight.

Can anyone speak to whether a javascript file can be included in this way in a Sandboxed Visual WebPart? If it should be working, any ideas on why it isn't?

If it cannot work this way, is there an alternative?


PS. Possibly related - Page.Header is null. This I discover when attempting to add an HtmlLink for a css file. Not sure what to think.

share|improve this question
Here's the MSDN documentation that mentions the sandbox ScriptManager restriction: – Stuart Pegg Jul 11 '12 at 11:01
And here's the bit that mentions Visual Web Parts are not supported in the sandbox:… – Stuart Pegg Jul 11 '12 at 11:03
Visual Webparts can be supported in Sandbox solutions. There is an addin located on Codeplex for this. – Mike T Jul 19 '12 at 18:00
up vote 5 down vote accepted

The Page object you're getting in the Sandboxed webpart isn't the real page, so this in one of the things that won't work.

Option 1: Render in RenderContents
You can manually render your script include in RenderContents:

protected override void RenderContents(HtmlTextWriter writer)
  string jQuerySrc = Page.ClientScript.GetWebResourceUrl(this.GetType(), "ProjectNS.Scripts.jquery.min.js");
  writer.WriteLine("<script type='text/javascript' src='{0}'></script>", jQuerySrc);

Option 2: Include from CDN
If your users have internet access then you can just put this into your .ascx file:

<script type="text/javascript" src=""></script>
<script type="text/javascript">
share|improve this answer
How do you prevent the resource from being loaded twice? – iOnline247 Mar 26 '13 at 12:47
There is no way you can make sure that the resource isn't loaded twice if it's something like jQuery where others may include it in their web parts. If you want to prevent yourselves from loading the resource twice then you need to render JavaScript which checks/sets a "global" flag and then load the resource dynamically if it's the first time on the page. – Per Jakobsen Mar 26 '13 at 19:04

This is the best solution I found:

It uses JS to add the script to the head element, and it also makes sure the resource is only loaded once.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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