Friday, July 16, 2010

SharePoint overriding the Core.css styling for specific site

The standard default core.css file is located in …\12\TEMPLATE\LAYOUTS\1033\STYLES. This STYLES folder is a virtually mapped via the /_layouts virtual directory that is mapped to each and every site in SharePoint.  So, the core.css file is accessible to every site by simply using the URL of “/_layouts/1033/styles/core.css”.  For every page that SharePoint renders, it renders a link to the core.css using this path. 

However, for any site that has been configured to use a custom core.css style sheet via the CustomizeCss method on the SPWeb class, the URL rendered in each page references a core.css file located in a different location.  This is accomplished in C# by the following code where web is an instance of the SPWeb class for the web site of your choice.

web.CustomizeCss(“core.css”);
web.Update();

The CustomizeCss(“core.css”) statement tells SharePoint to use a custom copy of the SharePoint core.css file.  Once this CustomizeCss method is executed, a copy of the core.css file found in the …\12\TEMPLATE\LAYOUTS\1033\STYLES folder is copied to the folder (access this by Sharepoint Designer) named "_styles" on the applied site.  

Modifying this core.css file will affect only the current site. 

Reverting this site back to using the original global (un-customized) copy of the core.css file can be accomplished just a easy using the following code.

web.RevertCss(“core.css”);
web.Update();

Executing the RevertCss method does not remove the custom core.css file on the web site’s “/_styles” folder.  It simply tells the site to no longer use it and to use the original core.css found in the C:\…\12\TEMPLATE\LAYOUTS\1033\STYLES folder; which is the same folder as the as the “/_layouts/1033/styles” folder.

So we now know how to use the CustomizeCss and the RevertCss methods on the SPWeb class to provide an easy way to point the site to a new custom style sheet file.

No comments:

Post a Comment