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 is the proper way (i.e. without having to manually copy the assets to any sharepoint folder or anything like that) to add custom javascript and CSS to a SP 2010 web part?

I would like them to be a part of the solution so that I don't have to deploy the solution and remember to copy/remove files anywhere.

Edit: If possible, please describe how to do that both in sandbox and non-sandbox solutions.

share|improve this question

4 Answers 4

If you're deploying the web part as part of a .wsp deploy the files to a mapped folder like _layouts as part of the solution. Just right click on the project in Visual Studio and click Add > SharePoint "Layouts" Mapped Folder. Then put the files you need in that folder and they'll be deployed during the solution install.

share|improve this answer
    
Then you can reference them with a relative url, eg: "_layouts/myscript.js" –  RJ Cuthbertson Jun 13 '12 at 18:24
    
Does this mean it cannot be a sandbox solution? If yes, is there a way to achieve this in a sandbox solution? –  andersonvom Jun 13 '12 at 18:45
    
christianfredh.com/blog/post/… according to this, not via this method. –  RJ Cuthbertson Jun 13 '12 at 18:52

Just use modules to add your assets (css and javascript). The best way to learn how to do, read Thomas Balkeståhl's blog.

enter image description here

And download his Visual Studio template and create your solution from his template.

share|improve this answer

For non-sandbox solution you can add JS and CSS files in the Layout mapped folder then use the SharePoint:ScriptLink tag for javascript files:

<SharePoint:ScriptLink id="ScriptLink1" runat="server" Localizable="false" Name="projectname/some-js-file.js" />

where the path projectname/some-js-file.js is inside the Layout folder.

For css files use the SharePoint:CssRegistration tag instead:

<SharePoint:CssRegistration ID="CssRegistration1" runat="server" Name="/_layouts/projectname/some-css-file.css" />

See here for more details: http://coderpills.wordpress.com/2014/06/19/include-javascript-and-css-files-in-your-sharepoint-2010-visual-web-part/

share|improve this answer

You can add Javascript using sandbox soltuion to one of your libraries. I have recently add JQuery library. Here are the steps from my blog post

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

share|improve this answer
1  
Since your blog post is just a few lines please just paste your content here and add a link to your blog for reference if it adds value. That way the content is here to help visitors, whereas links go stale. And you'll probably get upvotes instead of downvotes ;-) –  SPDoctor Feb 1 '13 at 21:50

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.