Managing CSS Files in Sitecore



Sitecore is a content management system, but can it also be a web design management system?  The short answer is that you can design some solutions to provide flexibility for content management users to manipulate the the design via CSS files, allow them to manage and upload their own CSS files or make changes to the initial CSS files.

Read more for the configuration steps required to support CSS files as Sitecore Media Assets.

You will need to modify the App_Config/MimeTypes.config file in your project.

Edit the following:

<mediaType extension="css">
   <mimeType>text/css</mimeType>
</mediaType>

Replace with the following:

<mediaType extension="css">
   <mimeType>text/css</mimeType>
   <forceDownload>false</forceDownload>
   <sharedTemplate>system/media/unversioned/file</sharedTemplate>
   <versionedTemplate>system/media/versioned/file</versionedTemplate>
</mediaType>

Here is some sample backend C# code of how you would pull in the CSS file:

FileField cssFileField = Sitecore.Context.Item.Fields["CSS"];
Item cssItem = cssfileField.MediaItem;
string cssUrl =  Utilities.GetAbsoluteUrl(Sitecore.Context.Page.Page.Request, StringUtil.EnsurePrefix('/',Sitecore.Resources.Media.MediaManager.GetMediaUrl(cssItem)));
cssElement.Attributes.Add("href", cssUrl);

Note: Only CSS files that are uploaded to the Media Library after this config change are affected.

Here is a link to a Sitecore Forum post about the topic:

http://sdn.sitecore.net/SDN5/Forum/ShowPost.aspx?PostID=36749


Categories:     

Add a comment
 
For Security please arrange
the letters to spell ‘OASIS’
  • O
  • A
  • S
  • I
  • S