No CssRegistration control in Sandbox

Hmm – and interesting problem; in the Sandbox you don’t have any access to the CssRegistration class. It’s in the Microsoft.SharePoint.WebControls namespace, and you don’t have access to that.

So, what do you do to link to an external stylesheet? Um, well, there isn’t any pretty story. The best I’ve come across is by Ian Chivers, who uses JavaScript to add another <Link> tag into the <Head> of the page.

Clever, but yuck!

The explanation that I came across by Mukul Sabharwal (and seemed pretty good) was:

Sandbox webparts do not execute in the same ASP.NET context as the page in which they reside, instead they execute in a distince ASP.NET context in a seperate worker process (UserCode Worker Process). SharePoint setups a make-believe world for the WebPart — specifically this make believe world consists of a single page with a single webpart zone and a single webpart — your sandbox webpart.

Once the webpart finishes execution the result is returned back to the main page. In fact, even if RegisterCssReference was not internal, there is no guarantee that the CSS could be applied, because we may have be too late in the ASP.NET page lifecycle to go do that.

This brings me to to answering your question, which is it is not currently possible to specify CSS into a page for a Sandbox Web Part.

Makes sense – but some way of registering CSS might be useful.

Edit: I tested Ali Robertson’s comment about just putting the link tag directly in the page, and that works.Unfortunately, there’s no way of then resolving Themed CSS files – something the CssRegistration class does – so you can’t have themed CSS in sandbox solutions (at least, as far as I can see at the moment).

No CssRegistration control in Sandbox

3 thoughts on “No CssRegistration control in Sandbox

  1. No reason to use JavaScript to place the tag in the page head: the link tag can exist anywhere on the page. You can just chuck it in where the webpart resides. The JS won’t be parsed any earlier than the tag, and all JavaScript is UI blocking code, so best avoided if unnecessary. Both ways are HTML5 compliant and work in all SP2010 supported browsers.

  2. Well, you know, I’d wondered that. I could only find references to putting the LINK tag in the head. I think I’ll try that; although the CssRegistration control would be more ideal, I can output a LINK without much hassle, even though I’d need to dynamically generate the URL.

  3. The same goes for the ScriptLink control then I suppose? Although adding a reference to an external JS file can be done in the very same manner, problems can occur especially when your web part uses jQuery to do some DOM manipulation. It would be nice for our web part to add files (css or js) to the head of the document.

    I found a work around where you can add custom action (script blocks) to your web parts elements.xml file – however I don’t believe there is a way to toggle custom actions with the code behind is there? Part of the web part options is a checkbox to include jQuery – very useful if the branded master page doesn’t already include it, or somebody doesn’t want the annoyance of my web part adding an additional reference to the file.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.