Skip Ribbon Commands
Skip to main content

Anders Rask on SharePoint


Anders Rask on SharePoint > Posts > Using CssRegistration to secure CSS isn't cached on clients
June 05
Using CssRegistration to secure CSS isn't cached on clients

​One of the controls that have received alot of TLC in Sharepoint 2010 is the CssRegistration control. The CssRegistration control had limited functionality in MOSS: it made sure that multiple registered CSS links was only rendered once, but often you want to control the order that these CSS pages are loaded in, and this is supported in SP2010 with the After attribute/property. Read more on the CssRegistration control in Wictor's excellent blog post here.

So far so good, we can control the order of the CssLink's, but what happens when you want to change the CSS files referenced? It is a returning problem that client browsers does not detect changes in CSS, and hence the cached CSS on the clients are not updated before somebody empties the cache for example by pressing CTRL+F5.

My friend Chris O'Brien have written an excellent blog post that describes the nuts and bolts regarding how to avoid CSS and JS being cached on the client.
So, out of the box CssRegistration solves this problem by adding ?Rev=<id> to the link url. But on several occasions I have noticed that SharePoint *does not* append the ?rev=<id> when using CssRegistration control, so what is going on here?
Example of what works and what doesnt:
This will not render the CSS with a ?rev=<id> tag:
<SharePoint:CssRegistration name="/_layouts/styles/MyCustomer/css/myCustomer.css" After="corev4.css" runat="server"/>
While this *will* render with a ?rev=<id> tag:
<SharePoint:CssRegistration name="MyCustomer/Css/myCustomer.css" After="corev4.css" runat="server"/>
I decided to dig into the code behind CssRegistration and CssLink to find out why.
Deep inside CssLink, the internal methods MakeCssUrl() and MakeLayoutsCssUrl() will check the Name attribute given from either CssLink control or CssRegistration control. If the link is "absolute" (starts with "/") the code simply returns the Name property and uses it without modifications, and hence without the ?rev=<id> tag!
For our second CssRegistration the code will eventually call MakeLayoutsCssUrl(String,Boolean) that will in turn call MakeLayoutsCssUrl(string cssFile, bool bInDesign, bool bLocalizable) with the third bLocalizable parameter hardcoded to true. This method will call the public static method SPUtility.MakeBrowserCacheSafeLayoutsUrl(string name, bool localizable) that in turn will render our CSS link with a nice ?rev=<id> tag.
Note: localization uses CurrentUICulture for determining which LCID folder it should render as link to the CSS. In this example CurrentUICulture is en-US, so the full path to the CSS file will become
Using Thread.CurrentThread.CurrentUICulture.LCID is problematic because SharePoint set this property when rendering the page depending on the current users regional settings. This means that calling code that utilizes CurrentThread.CurrentUICulture (and CurrentCulture) may fail if you dont run inside SPContext (like when calling code from PowerShell -theres a fix for that as well, but I will blog about that another time) if the culture varies from the Operating System language and regional settings.
Also it means that if you have more than one language pack installed on your farm, you need to provision your CSS file to every installed language, since end users can select the sites language to any language that site owner has made available! So even though everything looks fine, the second a user changes his language for example from Danish to English, and you have provisioned your CSS only to _LAYOUTS/1030/styles the whole site will fail to render (note that the page itself could be cached in the browser, so you might need to clear browser cache or do a CTRL+F5 to make the page fail -or render with localized CSS)!
So to sum things up: 
Use CssRegistration control to register your CSS files, but be sure to provision the CSS file inside the [LCID]/styles folder of LAYOUTS, or you will not get the ?rev=<id> parameter and hence have problems whenever you update your CSS.
Be careful when you deploy the CSS file. To be sure you need to deploy it to every installed language LCID folder, or you will find that CSS fails on certain sites for certain users (and make sure that if a language pack is installed later on, you must also deploy CSS to support that language)!
You can use PowerShell to find out what languages are installed on your farm:
LCID DisplayName
---- -----------
1033 English
1030 Danish
If you pointed to the wrong LCID ALL web frontends in your farm or your page will fail to render with a message that SharePoint could not create a cache safe URL address for the CSS file since it could not find it!


Re: Using CssRegistration to secure CSS isn't cached on clients

 on 9/2/2015 9:31 PM

Re: Using CssRegistration to secure CSS isn't cached on clients

Might be controlled the surface Using the correct  Css source  to secure CSS cached and explore Using the presentation prepared is important as the student needs to know the inside modifications careful deploy the CSS file.
 on 7/17/2018 5:37 AM

Re: Using CssRegistration to secure CSS isn't cached on clients

It idoesn't cache on clients, right? I knew it but otherwise! I was so worried and couldn't figure out what to do until I stumbled on this work, thanks!
 on 11/3/2018 9:23 AM

Laboratory Filtration Equipments

Hi, I think u have posted the best article according to information. Keep it up I will be glad to see such informative and helping article from u. very useful information. Thanks for sharing

 on 12/27/2018 2:26 AM

Laboratory Equipments

It was a decent post to be sure. I completely delighted in understanding it in my lunch time. Will definitely come and visit this blog all the more frequently. Much obliged for sharing.
 on 1/3/2019 4:17 AM

Re: Using CssRegistration to secure CSS isn't cached on clients

This sounds a book I might find interesting, happy to have found this survey. I think it will be an eye opener.
 on 1/15/2019 11:45 PM

Re: Using CssRegistration to secure CSS isn't cached on clients

I learn this post almost three times now and I am still not bored of it. This topic is something I really like a lot and I wish you could write more related this. I would like to be informed courtesy of your web log posts.
 on 1/15/2019 11:45 PM

Re: Using CssRegistration to secure CSS isn't cached on clients

Thanks a million for including my blog on your list. You are definitely one of my favorites! If you or your other followers are interested in
 on 1/22/2019 1:24 AM

Re: Using CssRegistration to secure CSS isn't cached on clients

Great inspiring article. I am really enjoy showing your post because its very unique information for me. Thanks a lot for sharing this magnificent post on
 on 1/22/2019 1:24 AM

Re: Using CssRegistration to secure CSS isn't cached on clients

I am definitely enjoying reading your article. You give us some great information and marvelous stories of
 on 1/22/2019 1:24 AM
1 - 10Next

Add Comment


Body *

Migrated Source URL

Commentator Name

Commentator Email

BotCheck *

Are you human? What is the sum of fifty-two minus ten?