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 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.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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.

Update

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" />
</Module>

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].

share|improve this answer
    
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. –  SPArchaeologist Nov 20 '12 at 15:13
add comment

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.