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

What would be the best approach in using/including jQuery in Office 365? Should I just use google CDN or is it possible to deploy the jQuery file to the server?

Is there any good articles/resources available on this topic?

Thank you.

share|improve this question

5 Answers 5

Sandbox mode implies you'll have to do it all at a site level. Personally I'd be going with a CDN link, and additionally I'd be checking to see if it's already been loaded elsewhere in the page - useful in the case of a jquery-dependent webpart which may be included two or three times in a page:

https://gist.github.com/902090

Of course, if the solution you're building is for general release, you'll want to include the files so that it'll still work in secured intranets which don't have a way to hit the CDN - in which case deploy to a library such as the style library (a common target for JQuery)

share|improve this answer
    
Very good solution. I have forked your gist and added some improvements: a window.onload isn't safe to use, you can overwrite some other functionality. myOnLoadEvent must run even if jQuery is defined. A protocol relative url is better than checking protocol. –  Anatoly Mironov Jun 13 '12 at 20:40

You have deploy jquery library to document library like style library and refer it from there. You can use CDN too if you prefer to do so.

share|improve this answer

The most preferred location to add JQuery is the "Site Asserts" library which exists in SharePoint Online - Office 365 site. Quick Steps to create a solution for this

  1. Create a Empty Solution in Visual Studio 2010 and deploy it as sandbox.
  2. Create a Folder called JQuery.
  3. In this Folder add an new Module. You would notice you get a new Elements.xml and a txt file. You can delete the txt file.
  4. Add the JQuery library in the "JQuery" folder.
  5. Next edit the Elements.xml to add the "URL" parameter, where the JQuery library will be deployed. You will notice that Elements.xml will now contain the Module element for JQuery.

Open elements.xml file and Change the following

  1. Edit Module Name - Add some name "JqueryModule"

  2. Add URL property to Module, something like URL ="Site Asserts"

  3. In File tag add Type=”GhostableInLibrary”

Thats it!

Reference http://preachingsharepoint.blogspot.ca/2012/05/add-jquery-to-office-365-sharepoint.html

share|improve this answer

I posted a blog artlice about adding a jQuery reference to Office365 which is essentially the same as a sandbox solution here http://blog.ianchivers.com/2012/04/add-jquery-to-office365-solutions.html.

Becuase of the restrictions on Sharepoint 2010 Sandboxed solutions it's essential to be able to write functional code client side, as you are very restricted on what you can do on the server.

Writing Javascript can be extremely labourious without the assistance of some helper libraries like jQuery, and adding javascript links and code is even a challenge in a sandboxed solution.

Here's a cool way of adding jQuery code to Office365 or Sharepoint Online sandboxed solutions.

protected override void RenderContents(HtmlTextWriter writer)  
        {  
            StringBuilder js = new StringBuilder();  

            js.AppendLine("$(document).ready(function(){");  
            js.AppendLine(" alert('hello world!');");  
            js.AppendLine("});");  

            base.RenderContents(writer);  

            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");  
            writer.AddAttribute(HtmlTextWriterAttribute.Src, "//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");  
            writer.RenderBeginTag(HtmlTextWriterTag.Script);  
            writer.RenderEndTag();  

            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");  
            writer.RenderBeginTag(HtmlTextWriterTag.Script);  
            writer.WriteLine(js.ToString());  
            writer.RenderEndTag();             
        }    

There's also a post about on how to add a stylesheet to a page from a Sharepoint 2010 sandbox webpart: http://blog.ianchivers.com/2011/11/add-stylesheet-to-page-from-sharepoint.html

share|improve this answer

You can add jQuery files in Style library through module and access it from style library.

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.