Friday, August 16, 2013

Get Document Icon Image in SharePoint

While we Query the Documents from the Library and list out the items with the Custom branding in the Webpart, Users expects to see the related icon for the document to easily segregate and recognize the document. SharePoint makes it easier with a method in SPUtility class called SPUtility.MapToIcon() which gets the document icon file name, which is being used in SharePoint. These icons are stored in 14 hive images folder.

string docicon = SPUtility.ConcatUrls("/_layouts/images",
SPUtility.MapToIcon(item.Web, SPUtility.ConcatUrls(item.Web.
Url, item.Url), "", IconSize.Size16));
SPUtility.MapToIcon() method retrieves the document from a XML file called “Docicon.xml” which maintains the mapping for the document icons with the extension files under 14 hive XML folder. We can also use the same XML file to get the respective icon for the Document based on the document extension. In case if you are listing the documents from some other source other than document library.
  private string ReturnExtension(string fileExtension)
     XmlDocument document = new XmlDocument();
     document.Load(SPUtility.GetGenericSetupPath(string.Empty) + @"\TEMPLATE\XML\DOCICON.XML");
     XmlNodeList xnList = document.SelectNodes("//DocIcons/ByExtension/Mapping[@Key='" + fileExtension.ToLower() + "']");
            string returnURL = "";
            if (xnList.Count > 0)
                foreach (XmlNode xn in xnList)
                    returnURL = xn.Attributes["Value"].Value;
                returnURL = "icgen.gif";
            return returnURL;
Related Posts Plugin for WordPress, Blogger...