Branding SharePoint 2010 Collaboration Sites – Part 2 in a Series
In part one of this series on branding SharePoint 2010 collaboration sites I posted a bit about how the new Theming engine works in SharePoint 2010. In this post, we’ll be going over the new and improved CSSRegistration control in SharePoint 2010.
A history lesson; the CSSRegistration control in SharePoint 2007
The CSSRegistration control in SharePoint 2007 basically has one property you can set, the Name is the Url to the CSS file that you want to register. When you register a style sheet through the control, it adds the CSS file to an alphabetically sorted list. The style sheets in this list are then emitted as HTML <link> elements by the CSSLink control.
If you required one CSS file to be emitted before another, you would need to name them accordingly. For example:
<SharePoint:CSSRegistration Name="foo.css" runat="server" /> <SharePoint:CSSRegistration Name="bar.css" runat="server" />
Even though I registered foo.css before bar.css, the HTML on the rendered page would look like so:
<link rel="stylesheet" type="text/css" href="bar.css"/> <link rel="stylesheet" type="text/css" href="foo.css"/> <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/core.css?rev=..."/>
bar.css would come before foo.css and what’s this core.css doing here? Somehow, someone at MS decided that the core.css containing ALL of the base CSS rules for WSS should override any custom CSS!
As a workaround, you could set the DefaultUrl property for the CSSLink control to point to a single CSS file that would come after core.css, and then use @import to link to any additional custom CSS files.
The fact that core.css was emitted after any style sheets registered using CSSRegistration made the CSSRegistration control all but useless in 2007.
The shiny new CSSRegistration control in SharePoint 2010
So things are much better in the SharePoint 2010 world. If you examine the documentation for the SharePoint 2010 version of the CSSRegistration control, you’ll find the addition of the following properties, which currently have no descriptions to their usage in the SDK:
RevealToNonIE (boolean)
I assume this property would allow you to register IE-only style sheets. This doesn’t seem to be working in Beta 2 though as it doesn’t matter whether I set this to true or false, the CSS files would get emitted regardless of the browser I was using.
ConditionalExpression (string)
Another assumption that this would allow us to set certain conditions in order for the style sheet to appear in HTML. Nothing at all in the way of documentation nor could I find any examples in the 14 Hive as of Beta 2.
This property is an Internet Explorer Conditional Comment. For example, if we wanted to link to a style sheet specific to IE 7 or greater, we can do this:
<SharePoint:CSSRegistration Name="foo.css" ConditionalExpression="gte IE 7" runat="server" />
The following markup would be emitted:
<!--[if gte IE 7]> <link rel="stylesheet" type="text/css" href="foo.css"/> <![endif]-->
After (string)
This property is what was sorely missing in 2007. We can now tell the CSSLink control to emit the registered style sheet after another CSS file. You can either define just the leaf filename, ie bar.css or the path to the CSS file, ie /foo/bar.css. If you don’t use the After property, style sheets will still be emitted by alphabetical order. For example:
<SharePoint:CSSRegistration Name="bar.css" After="foo.css" runat="server" /> <SharePoint:CSSRegistration Name="foo.css" runat="server" />
bar.css is emitted after foo.css because we specified the After property when we registered bar.css:
<link rel="stylesheet" type="text/css" href="foo.css"/> <link rel="stylesheet" type="text/css" href="bar.css"/> <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css?rev=..."/>
corev4.css IS STILL emitted after everything else for whatever reason, unless you use the After=”corev4.css” property when registering AND there are a few gotchas that you need to be aware of.
Gotcha 1: You’re never sure of the sort order
As of Beta 2, there’s some weirdness when you register multiple style sheets that all require to be after the same CSS file. They are no longer sorted alphabetically but in most cases they end up being last in first out:
<SharePoint:CSSRegistration Name="1.css" After="corev4.css" runat="server" /> <SharePoint:CSSRegistration Name="2.css" After="corev4.css" runat="server" /> <SharePoint:CSSRegistration Name="3.css" After="corev4.css" runat="server" />
I was expecting 1, 2, 3… but instead I get 3, 2, 1…
<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css?rev=..."/> <link rel="stylesheet" type="text/css" href="3.css"/> <link rel="stylesheet" type="text/css" href="2.css"/> <link rel="stylesheet" type="text/css" href="1.css"/>
Gotcha 2: I need something AFTER another thing…
The other thing to take note on is when you need to define nested After. For example, I need to make sure that the style sheets are emitted in the order 1, 2, 3, 4:
<SharePoint:CSSRegistration Name="1.css" runat="server" /> <SharePoint:CSSRegistration Name="2.css" After="1.css" runat=â€server" /> <SharePoint:CSSRegistration Name="3.css" After="2.css" runat="server" /> <SharePoint:CSSRegistration Name="4.css" After="3.css" runat="server" />
We don’t get what we would expect the result to be, instead it comes out as 1, 4, 3, 2:
<link rel="stylesheet" type="text/css" href="1.css"/> <link rel="stylesheet" type="text/css" href="4.css"/> <link rel="stylesheet" type="text/css" href="3.css"/> <link rel="stylesheet" type="text/css" href="2.css"/>
It looks like as a general rule of thumb, you should register the style sheet which comes after another one FIRST. So in order to get 1, 2, 3, 4 we need to define our registration like so:
<SharePoint:CSSRegistration Name="4.css" After="3.css" runat="server" /> <SharePoint:CSSRegistration Name="3.css" After="2.css" runat="server" /> <SharePoint:CSSRegistration Name="2.css" After="1.css" runat="server" /> <SharePoint:CSSRegistration Name="1.css" runat="server" />
EnableCssTheming (boolean)
Remember those “Themable folders” and compile time directives in the CSS files I mentioned about in Part 1? The EnableCssTheming property tells the SharePoint 2010 theming engine to recompile the style sheet using any directives found within the registered CSS file if the file’s location is in one of those “Themable” folders. EnableCssTheming is “true” by default, you have to explicitly set it to “false” if you do not want the registered style sheet to be parsed and re-compiled.
For example:
<SharePoint:CSSRegistration Name="<% $SPUrl:~sitecollection/_layouts/1033/Styles/Themable/foo.css %>" runat="server" />
If we don’t have a theme selected, the HTML emitted will be:
<link rel="stylesheet" type="text/css" href="/_layouts/1033/Styles/Themable/foo.css"/>
But if there’s a theme applied to a site, it will be in one of two locations, where [1234567] and [12345678] are hex values that represent the theme name and CSS file paths. The ctag QueryString parameter is a simple integer counter used for cache invalidation. It increments up every time a web’s theme gets changed.
If the theme was applied through the web UI:
<link rel="stylesheet" type="text/css" href="/[WEB PATH]/_themes/[COUNTER]/foo-[12345678].css?ctag=[COUNTER]"/>
Or, if the theme was set programmatically using the ThmxTheme class and shareGenerate was defined as “true” it will be located here:
<link rel="stylesheet" type="text/css" href="/_catalogs/theme/Themed/[1234567]/foo-[12345678].css?ctag=[COUNTER]"/>
Just to re-iterate, the two three locations where the “Themable” folder can be located are:
- In the “14 hive” at %ProgramFiles%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\[LCID]\STYLES\Themable;
Where [LCID] is the Locale ID. In North America, the default is “1033” for English (US).
- In each web’s Style Library/Themable. These CSS files only affect the specific web and all languages.
- In each web’s Style Library/[LANG]/Themable.
These CSS files only affect the specific web and a specific language. In North America, the default is “en-US“
You can place your CSS files inside sub-folders of Themable for neatness but not the other way around… /Style Library/Themable/foo/bar.css will be recompiled by SharePoint 2010’s theming engine while /Style Library/foo/Themable/bar.css will not.
In the next episode…
Whew! We’ve now gone through how to register a style sheet so that it’s compatible with the new SharePoint 2010 theming engine.
My next post will be how to get our custom style sheets onto the out-of-the-box collaboration sites without having to create custom site templates or master pages. We’ll revisit an old friend (or new acquaintance to some), the Delegate Control!
UPDATE:
You are a SharePoint branding animal … keep it coming!
Hi,
This artical is very useful for me. I am a Share Point developer and always looking to
learn something new. I would like to introduce another good SharePoint blog, Have a look.
http://SharePointBank.com
Harry
Kindly publish your next post at the earliest possible. It’s a requirement of my Project.
Thanks and Regards,
Khyati
Hi Khyati,
Sorry for the delay! Part Three has now been published!
Hi, thanks for your post.
Im’w working on the Rtm and I’m trying to let the “after” attribit working, but it doesn’t seems to behave linke I would and as you explained.
In particular, this is what I wrote:
<SharePoint:CssRegistration ID="cssMain" name="” After=”Navigation.css” runat=”server”/>
<SharePoint:CssRegistration ID="cssNavigation" name="” After=”jquery.theme.black.css” runat=”server”/>
<SharePoint:CssRegistration ID="cssJQueryTheme" name="” After=”jquery.pager.css” runat=”server”/>
<SharePoint:CssRegistration ID="cssJQueryPager" name="” After=”corev4.css” runat=”server” Version=”4″/>
But this is what I got on the page rendering:
So I didn’t get the reverse order as you explained in your post…
I tried also to specify in every CSSRegistration After=”corev4.css” in the same reverse order as above: but nothing changed in the rendering page.
Any ideas?
Thanks
Bi
I found out that in Sharepoint 2010, if you have your custom style sheet set in Site Settings > Look and Feel > Master page > Alternate CSS URL then you DO NOT need CSSRegistration in v4.master file.
I was having issues and wondering why my stylesheet was not being applied with theming. Viewing the source code, I realized it was in there twice, so I had to look through and find the duplicate. I had my custom css file under Site Settings as well as with CSSRegistration. If you remove one, everything works as expected (in my case I removed CSSRegistation), but with both in there you may not see theming work. That was my problem and it may help someone else.
Thought I would post in case someone else was having the same problem.
Hi,
This Article was very very interesting.. I am a Designer working for SP2010 Branding.. I have a query.. I am developing a custom site in SP2010.. so the look and feel is unique, and have a custom master page.. We will be developing this site for different languages as well.. But I want the all the language to refer to single CSS, my CSS is taking care of the look and feel of the site it need not be language specific. The UI is not changing for different language, is it possible to put my CSS, Scripts and Images out of 1033 so that i only need to manage single CSS, Images and scripts for every language..
When I link CSS and Scripts using CSSREGISTRATION SP2010 will it looking for styles & scripts under Language specific folders?
I am planning to put the CSS under
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\”CUSTOMSITENAME_STYLES”\”CUSTOMSTYLE.CSS”
and Scripts Under
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\”CUSTOMSITENAME_SCRIPTS”\”CUSTOMSCRIPT.JS”
can i use
<SharePoint:CSSRegistration Name="” runat=”server” />
Will My Master page link the CSS properly and will not be language specific
Thanks
Kiran S
reposting end part as i guess the css registration was removed from comment.. trying agen
can i use
SharePoint:CSSRegistration Name=”” runat=”server”
Will My Master page link the CSS properly and will not be language specific
Thanks
Kiran S