LoginLogin|RegisterRegisterTuesday, February 07, 2012
bWare TechnologiesLet us improve your Microsoft Windows SharePoint Services experience, one web part at a time!Bookmark and Share
 Search
  
   
 
Jul23

Written by: Bruce Dishongh
7/23/2009 12:39 PM RssIcon

If you have ever had the need to locate the icon that Windows SharePoint Services 3.0 or MOSS 2007 uses to represent a specific List Item you have probably come across the method MapToIcon of the SPUtility class in the Microsoft.SharePoint.Utilities namespace. A quick search on your favorite search engine will confirm that there a few Blog posts on the matter, and they show us how to use the method as stated in the Microsoft Windows SharePoint Services 3.0 SDK. But do you really know this method?

SDK documentation excerpt…

SPUtility.MapToIcon(SPWeb, String, String)
Returns the name of the image file for the icon that is used to represent the specified file.

The SDK documentation shows that we only need 3 parameters for our method…an SPWeb object that represents Site that contains the file, a String that contains the name of the file and a String that is the ProgId of the application that created the file. However, the method will return an icon reference with or without the last parameter ProgId, and it get it right...most of the time. That's right, only most of the time. In the function below we intentionally ignored the last parameter of the SPUtility.MapToIcon method, yet it still manages to retrieve the correct icon for the Microsoft Word 2007 document.

Function MapToIconExampleNoProgId(ByVal Web As SPWeb, ByVal FileLeafRef As String) As String
   Return SPUtility.MapToIcon(Web, FileLeafRef, "")
End Function

Sample
 
For the example web part pages (in the graphic above) we only got back a reference to a generic HTML document icon. Not bad, but not the same icon retrieved by the SharePoint web part just below our example. The Windows SharePoint Services web part retrieved the correct Microsoft SharePoint Designer 2007 icon as you would expect. So provide the correct ProgId to the method SPUtility.MapToIcon and be done with it...right! Well that is what I thought. This is where I began to run into a little trouble...since I was using SPSiteDataQuery to retrieve List items I tried the most obvious choice...the view field ProgId in my query. This view field returns the ID of a list item in the format ID;#, not the actual ProgId I was expecting. After some digging, I finally found the ProgId property I was looking for in the SPFile class. A code example is below...but notice the line gsDocIconUrl = objFile.IconUrl!
 
Sub ExampleIconUrl()
   
Dim objSite As SPSite = New SPSite(gsSiteUrl)
   
Dim objWeb As SPWeb = objSite.OpenWeb
   
Dim objFile As SPFile = objWeb.GetFile(gsCtxUrl)

   
If Not objFile Is Nothing Then
      
gsDocIconUrl = objFile.IconUrl
   End If

   ' Cleanup...
   objWeb.Dispose()
   objSite.Dispose()
End Sub

After having come this far to grab the ProgId...it turns out we don't need it at all. The SPFile class has the property IconUrl which gives us the icon we were looking for in the beginning. So do we need the SPUtility.MapToIcon? Certainly, but you may need to utilize both options depending on your situation. In my case since I was unable to retrieve the ProgId from existing information, I was unable to successfully retrieve the ProgId from a SPSiteDataQuery query, so I had to go hunt for the ProgId with more code. Also keep in mind that according to the Windows SharePoint Services 3.0 SDK documentation the SPFile class only covers web part pages, items in Document libraries, and files in Folders so it wouldn't be any help to you for a Task item. After more testing it turns out that using the SPUtility.MapToIcon method is accurate for most all Content Types without providing a valid ProgId parameter. And where it leaves off the SPFile class can get you those icons for those web part pages, items in Document libraries, and files in Folders.
 
One last note...the SPUtility.MapToIcon method does not require elevated privileges while the method SPFile.GetFile() does. So be careful, if you have a web part that should display an icon for an item to a restricted account, perhaps even anonymous account, you will have to run the SPFile.GetFile() method with elevated privileges, our get the dreaded login popup. My example ExampleIconUrl() sub is written to be run with elevated priviliges...the GS prefixed variables are class level variables.


Technorati Tags: SharePoint,WSS 3.0,MOSS 2007,WebPart,Web Part,MapToIcon

Tags:
Categories:

Your Name:
Gravatar Preview
Email Address:
(Optional) Email used only to show Gravatar.
Your Website:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment  Cancel 



 Blog Search:


Announcements

 
Support FAQ's
SkinImage
SkinImage
Our FAQ's include up-to-date details about our products including information about installation, product usage, licensing and support.

FAQ's
SkinImage
 
Community Forums
SkinImage
SkinImage
Our peer-to-peer by Community Forums allow customers to exchange valuable information with our developers on general technical issues and product-specific topics.

Product Forums
SkinImage
 
Support Request
SkinImage
SkinImage
Licensed customers can use the online Support Request Form to submit support requests. Our online support ticketing system is the best way to ensure that your requests are properly tracked and answered by our Technical support team.
SkinImage
 
 
 
Microsoft® Startup Zone Privacy-Policy-By-TRUSTe
 
 
Privacy Policy by TRUSTe | Terms Of Use | Site Map
 
© 2009-2011 bWare Technologies, LLC. All rights reserved.