Thursday, January 28, 2010

Using Sharepoint Icons


SharePoint already provides icons for files, mainly in lists. You can also make use of those same icons, for example, for custom web parts or controls.


The icon images are stored in the images folder in the 12 hive:
Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\IMAGES

This maps to the virtual directory:
/_layouts/images
There is also an XML file with information of how to map a file type or extension  with the appropriate icon. This file can be found at:
Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML\DOCICON.xml

If you want to add icons for unknown file types or update existing icons, you can customize theDOCICON.xml file and place the image files in the above-mentioned images folder.
To do this association programmatically, SharePoint provides the method MapToIcon available in theSPUtility class in the Microsoft.SharePoint.Utilities namespace.
To retrieve the path for the icon image, the following code can be used:
string iconPath = "/_layouts/images/" + SPUtility.MapToIcon(web, filename, string.Empty);
  1. The first parameter is the web where the file is located (usually SPContext.Current.Web).
  2. The second is the filename for the file (e.g. "test.docx").
  3. The third parameter is an ID "of the application that was used to create the file" (you can provide an empty string for this argument).
To help retrieve the filename of a file, there is also another useful method: GetUrlFileName. By passing the URL to this method, it retrieves the filename. E.g.:
string filename = SPUtility.GetUrlFileName("http://test.mossdevdomain.com/SiteCollectionDocuments/test.docx");
Enjoy!

No comments:

Post a Comment