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! Continue reading “No CssRegistration control in Sandbox”

Advertisements
No CssRegistration control in Sandbox

Adding CSS links to your SharePoint pages or code…

I love Twitter – you get some though provoking questions on it. Thomas Resing asked the other day:

How are you applying styles to your custom web parts in #SharePoint? using CSSClass property, stuck on deploying css <Link> tag in the head

Well, for pages themselves you have the CSSRegistration control. It’s an ASP.NET control, and looks something like:

<SharePoint:CssRegistration name="/_layouts/myBrand/SomeStyle.css" runat="server"/>

This control registers the URL with the page, but doesn’t emit anything. That’s controlled by the CssLink control:

<Sharepoint:CssLink runat="server" />

It actually outputs the <link> elements, and this means that you can have a CSSRegistration control low down in the page (in a Content control, for example) and you can still output it in the <head> of your page. There’s a good post about this on CleverWorkarounds.

So, could we use that control? Well, we should be able to instantiate one – except it turns out that we don’t have to. We can just use the CSSRegistration.Register() static method – something like:

protected override void CreateChildControls(){
CssRegistration.Register("/_layouts/myBrand/SomeStyle.css");
}

Job done…

Adding CSS links to your SharePoint pages or code…