I have created a feature in sharepoint 2010. The feature has a visual web part (sandboxed), the web part has controls etc.

I have added some styles to the web part. How do i upload the stylesheet to sharepoint 2010? This must be done via visual studio 2010.


One way to do this is to use CustomAction and ScriptBlock, it's not pretty, but it does work.
Add a normal Module and add this:

<CustomAction Location="ScriptLink" Sequence="SOME NUMER"
  ScriptBlock=";(function(){var l=document.createElement('link');l.rel='stylesheet';l.href='PATH TO CSS';document.getElementsByTagName('head')[0].appendChild(l);})();" />

Replace SOME NUMBER and PATH TO CSS with the values you need.


For actually placing the CSS file on server you could use the same Module.

<Module Name="YourModule">
  <File Path="YourModule\path\css\style.css" Url="path/css/style.css" />

You could also set Type="GhostableInLibrary" and IgnoreIfAlreadyExists="TRUE" but since it's a Sandboxed Solution I don't think that would matter [reference needed].

  • Thanks. i know how to reference the stylesheet. What i need to know is how do i upload it to sharepoint via a visual studio feature? – user1786107 Nov 18 '12 at 9:30
  • Ah, sorry for misunderstanding. You can do it in the module as I mentioned in the answer. Let me update – eirikb Nov 18 '12 at 9:33
  • Thanks for accepting :) Just a note, I prefer to add scripts and stylesheets in a Module because then I know they will be referenced only once (ish). If you reference directly in your visual Web Part then adding the same Web Part twice to a page might cause havoc (for scripts). – eirikb Nov 19 '12 at 10:11
  • +1 - never thinked of adding the script with a custom action. Always used a delegate control with a "injector" user control. – Hitodama Nov 20 '12 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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