Die deutschsprachige Community für SharePoint, Microsoft 365, Teams, Yammer und mit Azure
How to add DocIcons to a SPGridView used in a SharePoint web part


Oliver Wirkus



Web 2.0



 Add to Technorati Favorites


 Delicious Bookmark this on Delicious


  Locations of visitors to this page


Recently I developed a SharePoint web part that is based on a SPGridView. I used the SPGridView to show documents that have been retrieved from other sites based on several conditions. One challenge has been: in the first column should show the DocIcon of the documents that have been added to the SPGridView.


This how the web part should look like (as an example):



First step: you need to add an appropriate column to the SPGridView to handle the different DocIcons. I have added these lines of code to the method that sets up the SPGridView:


The first column of the SPGridView is based on a ButtonField control that is set up to behave as an image. This control is used to show the DocIcon.


A DocIcon is not a static icon. It is bound the the corresponding document type. Therefore – you first need to add the document to the SPGridView before you can assign the DocIcon.


Second step: hook on the event RowDataBound of the SPGridView. This event is called when a new row of data has been added to the SPGridView. In other words: each time a new document is added to the SPGridView this event is fired.

I’m using this event to attach a DocIcon to each row – based on the document (or its type) that has been added before.

Here is a short example of how this could be implemented:


I just take the name of the document (e.g. wirkus2.txt) from the rows too, because I added the document’s name to the SPGridView before. Although the SPGridView only shows the document’s name (without extension), the full path to the document is available too, because I used a HyperLinkField as a second column to add the documents. The HyperLinkField shows the document’s name, but also holds the document’s full path (property DataNavigateUrlFields). That’s why I’m able to have both: the document’s name and it’s path with it’s filename and extension.

To retrieve the standard DocIcon from SharePoint I used SPUtility.MapToIcon(). This gives me the icon’s filename that corresponds to the given document type. The icon’s name is used to create a relative URL to _layouts\images. Last thing to do: assign this relative URL to the ButtonField control’s property ImageUrl. This control has been added to the SPGridView as first column (refer to screenshot 2) in the first step.

Bereitgestellt 14 Mrz 2014 10:14 von Oliver Wirkus
Gespeichert unter: , ,