Getting the theme icons from Sitecore

One of the new improvements of Sitecore 6.2 is that Sitecore has packaged the theme icons (.png’s) into .zip files. This means that you have about 27.000 files less when deploying your Sitecore solution.

But how do you access the icons? You have no longer direct access to the file. Well, Sitecore have supplied us with a ThemeManager in the Sitecore.Data.Managers namespace. The ThemeManager does not return the path to an icon. Instead, it returns the full HTML <img> tag. Hmm…

Here is 3 examples on how to use the ThemeManager:

// Return the images for all languages in Sitecore:
foreach (Sitecore.Globalization.Language language in LanguageManager.GetLanguages(Sitecore.Context.Database))
  string imgTag = ThemeManager.GetLanguageImage(language, database, 32, 32);
  // imgTag contains the full image tag:
  // <img src="/temp/IconCache/flags/32x32/flag_USA.PNG" width="32" height="32" alt="" border="0" />

// Return the icon of the current item
ThemeManager.GetIconImage(Sitecore.Context.Item, 32, 32, "", "");

// Return an arbitrary icon
ThemeManager.GetImage("Applications/16x16/about.png", 32, 32);

You can use regular expressions to take the file path from the image tag. This is some code I have stolen from a friend of mine. The GetIcon() function returns the path to an icon:

// Function to return the path to a Sitecore icon.
// Usage: GetIcon("Applications/16x16/about.png");
public string GetIcon(string iconPath)
  string imgTag = ThemeManager.GetImage(iconPath, 32, 32);
  string imgSrc = MatchInStringEnclosedBy(imgTag, "src=\"", "\"");

private string MatchInStringEnclosedBy(string content, string startPattern, string endPattern)
  StringBuilder pattern = new StringBuilder();
  return MatchInString(pattern.ToString(), _GROUPNAME, content);

private string MatchInString(string pattern, string groupName, string content)
  Regex regex = new Regex(pattern, RegexOptions.ExplicitCapture);
  Match match = regex.Match(content);
  if (match.Success)
    return match.Groups[groupName].Value.Trim();
  return string.Empty;

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6 and tagged , , . Bookmark the permalink.

3 Responses to Getting the theme icons from Sitecore

  1. Matt Kenny says:

    It’s not quite the same, but if you just want to use an icon on a web page, rather than getting the ThemeManager to give you a whole image tag, you can use the following as a src:


    Like the ThemeManager, it gets the icon out of the zip file and puts it into the temp folder. No extra code required. I’m not sure what version it appeared in, but it’s present in 6.2 and 6.3.

  2. Michael Wiesinger says:

    wow, love this post. sitecore api is so awesome. thx a lot

  3. Anders Laub Christoffersen says:

    Great post!

    To get the url for an icon image you can also just use:


    Works in Sitecore 6.6 and most likely also older versions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s