SharePoint. Getting document icon URL

docicon.xml

All the icons for files on SharePoint are registered in the file, located at {SharePointRoot}\14\TEMPLATE\XML\DOCICON.XML. Here is a small piece of it:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DocIcons>
  3.     <ByProgID>
  4.         <Mapping Key="Excel.Sheet" Value="ichtmxls.gif" EditText="Microsoft Excel" />
  5.     <!-- ... -->
  6.     </ByProgID>
  7.     <ByExtension>
  8.         <Mapping Key="zip" Value="iczip.gif" OpenControl=""/>
  9.         <!-- ... -->
  10.     </ByExtension>
  11.     <Default>
  12.         <Mapping Value="icgen.gif"/>
  13.     </Default>
  14. </DocIcons>

To read this data here is a static class:

  1. /// <summary>
  2. /// Document icons from DOCICON.XML file
  3. /// </summary>
  4. public static class DocumentIcons
  5. {
  6.     private static NameValueCollection ExtensionImages { getset; }
  7.  
  8.     static DocumentIcons()
  9.     {
  10.         ExtensionImages = new NameValueCollection();
  11.         // Path to docicon.xml file
  12.         var dociconPath = SPUtility.GetGenericSetupPath(@"template\xml") + @"\docicon.xml";
  13.         var fi = new FileInfo(dociconPath);
  14.         using (var stream = fi.OpenText())
  15.         {
  16.             var doc = XDocument.Load(stream);
  17.             var icons = doc.Element("DocIcons");
  18.             var byExt = icons.Element("ByExtension");
  19.             foreach (var ext in byExt.Elements())
  20.             {
  21.                 var key = ext.Attribute("Key").Value;
  22.                 var val = ext.Attribute("Value").Value;
  23.                 ExtensionImages.Add(key, "/_layouts/images/" + val);
  24.             }
  25.         }
  26.     }
  27.  
  28.     /// <summary>
  29.     /// Getting document icon URL by file extension
  30.     /// </summary>
  31.     /// <param name="extension">Extension without dot</param>
  32.     /// <returns>URL of icon</returns>
  33.     public static string GetIconUrl(string extension)
  34.     {
  35.         return ExtensionImages[extension] ?? "/_layouts/images/ICGEN.GIF";
  36.     }
  37. }

You can use it, for example, by creating a property in your class representing any document like this:

  1. /// <summary>
  2. /// Icon URL
  3. /// </summary>
  4. public string IconUrl
  5. {
  6.     get
  7.     {
  8.         // Get file extension
  9.         var ext = System.IO.Path.GetExtension(ServerUrl);
  10.         if (ext.Length > 0)
  11.         {
  12.             // Cut off leading dot
  13.             ext = ext.Substring(1);
  14.             // Return icon&for extension
  15.             return DocumentIcons.GetIconUrl(ext);
  16.         }
  17.         // Return default icon
  18.         return DocumentIcons.GetIconUrl(string.Empty);
  19.     }
  20. }

PDF-file icon

To display the icon for pdf-files in SharePoint document library, it is enough to register it in the ByExtension section:

  1. <Mapping Key="pdf" Value="icpdf.png"/>

And place icon into {SharePointRoot}\14\TEMPLATE\IMAGES folder. Even just such: .


Share

Comments