7

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.

4 Answers 4

6

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.

3
  • Then you can reference them with a relative url, eg: "_layouts/myscript.js" Commented Jun 13, 2012 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? Commented Jun 13, 2012 at 18:45
  • christianfredh.com/blog/post/… according to this, not via this method. Commented Jun 13, 2012 at 18:52
3

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.

2

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://blog.netgloo.com/2014/06/19/include-javascript-and-css-files-in-your-sharepoint-2010-visual-web-part/

-2

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

1
  • 2
    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
    Commented Feb 1, 2013 at 21:50

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.