0

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.

1 Answer 1

2

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

4
  • 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? Nov 18, 2012 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, 2012 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, 2012 at 10:11
  • +1 - never thinked of adding the script with a custom action. Always used a delegate control with a "injector" user control.
    – SPArcheon
    Nov 20, 2012 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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