Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

Thanks!

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
1  
Here's the MSDN documentation that mentions the sandbox ScriptManager restriction: msdn.microsoft.com/en-us/library/gg615454.aspx#BKMK_SplitPage –  Stuart Pegg Jul 11 '12 at 11:01
1  
And here's the bit that mentions Visual Web Parts are not supported in the sandbox: msdn.microsoft.com/query/… –  Stuart Pegg Jul 11 '12 at 11:03
2  
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

2 Answers 2

up vote 4 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);
  base.RenderContents(writer);
}

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="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
  jQuery.noConflict();
</script>
share|improve this answer
    
How do you prevent the resource from being loaded twice? –  iOnline247 Mar 26 '13 at 12:47
2  
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: https://github.com/eirikb/sppreload

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

 
discard

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.