Get document icon image in SharePoint

By Jatin Prajapati

How to get the document related icon from the the images directory in SharePoint.

In SharePoint 2007 or SharePoint 2010, while writing the code for listing out the document library items for custom user experience, we need to display the document item related icons if required. So, we always use the generic way of getting the document icon and based on that we provide if else or switch case to check and get the document icon. But we can directly get the icon information based on the document library item using SPUtility.MapToIcon method. See the below code example.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Reflection;
using System.Data;
using Microsoft.SharePoint.Utilities;

namespace JatinPrajapati
    public class ListDocumentLibItemsWebPart : WebPart {
        protected override void CreateChildControls()
            SPGridView gv = new SPGridView();
            gv = new SPGridView();
             gv.ID = "gv";
            gv.EmptyDataText = "No items found!";
            gv.AllowSorting = true;
            gv.AllowFiltering = true;
            gv.AutoGenerateColumns = false;
            gv.EnableSortingAndPagingCallbacks = true;
            CreateBoundField("Date", "Date", false, gv);
            CreateBoundField("Title", "Title", false, gv);
            CreateBoundField("Type", "Type", false, gv);

            SPWeb web = SPContext.Current.Web;
            SPListItemCollection items = web.Lists["My Documents"].Items;
           DataTable dt = new DataTable();
            foreach (SPListItem item in items) {
               DataRow dr = dt.NewRow();
                 dr["Date"] = item["Created"].ToString().Split(' ')[0];
                 dr["Title"] = item.Title;
                 string docicon = SPUtility.ConcatUrls("/_layouts/images",
                        SPUtility.MapToIcon(item.Web, SPUtility.ConcatUrls(item.Web.Url, item.Url), "", IconSize.Size16));
                 dr["Type"] = string.Format("<img src='{0}' />", docicon);
            gv.DataSource = dt;

         private void CreateBoundField(string sDataField, string sHeaderText, bool bReadOnly, SPGridView theGridView)
           BoundField field = new BoundField();
            field.DataField = sDataField;
             field.HeaderText = sHeaderText;
             field.HtmlEncode = false;
            field.HeaderStyle.ForeColor = ColorTranslator.FromHtml("#808080");
            field.HeaderStyle.Font.Names = new string[] { "verdana", "arial", "helvetica", "sans-serif" };
            field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
            if (sHeaderText.ToLower().Equals("type"))
                 field.ItemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                 field.ItemStyle.Width = Unit.Pixel(35);
                 field.HeaderStyle.Width = Unit.Pixel(35);
            field.ReadOnly = bReadOnly;
             if (!bReadOnly)
                 field.SortExpression = sDataField;
             field.Visible = true;

Get document icon image in SharePoint  (3365 Views)