SharePoint: Mapping Document File Extensions to Document Images


Mapping document file extensions to their corresponding image file names can be done quite easily using the XSLT extension functions found in the DDWRT namespace, namely the MapToIcon function. The DDWRT namespace maps to the DdwRuntime class found in Microsoft.SharePoint.dll in the Microsoft.SharePoint.WebPartPages namespace.

Incidently, the reference for the functions found in the DDWRT namespace can be found here.

This MapToIcon function has the following signature;

public string MapToIcon(string szProgID, string szExt)

You pass in either/or/both a ProgID or file extension (minus the .) and it returns the image file name for the document file extension, which should be found in the …/_layouts/Images/ folder.

What if you want to do this from managed code, should be easy right?

Well it is, kind of, if you know the secret handshake.

First, the DdwRuntime class is marked internal so you can’t just spin up an instance, you’ll have to use reflection.

Second, having done this, you call the MapToIcon method passing in either a ProgID or file extension and it returns null.

What gives? calling say FormatDateTime(…) works ok, but not MapToIcon(…).

I know this function works, so time to crack open JustDecompile/Reflector and dig out MapToIcons soft gooey bits.

It turns out that DdwRuntime loads and parses the docicons.xml file, found in {12 hive}/TEMPLATES/XML, but for some reason it relies on a concrete instance of the BaseXsltDataWebPart class, via the DdwRuntime.Wp property to provide the source text of that file. If you just spin up a instance of DdwRuntime, the Wp property will be null, and when called, MapToIcon catches the NullReferenceException and returns null.

So you also then, have to create a concrete instance of BaseXsltDataWebPart (which is abstract so you can use DataViewWebPart) and set the Wp property of your DdwRuntime instance before calling MapToIcon, after which it returns the expected result.

Heres the full code snippet;

const string fmt =
	@"<IMG title=""{0}"" border=""0"" alt=""{0}"" src=""/_layouts/images/{1}"">";
var ddwrt =
	Assembly.Load(
		"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c").GetType(
			"Microsoft.SharePoint.WebPartPages.DdwRuntime")
		.GetConstructor(Type.EmptyTypes)
		.Invoke(null);
var type = ddwrt.GetType();
var mi = type.GetMethod("MapToIcon");
var pi = type.GetProperty("Wp");
pi.SetValue(ddwrt, new DataViewWebPart(), null);

var ext = "pdf";
var imgName = mi.Invoke(ddwrt, new object[] {"", ext});
var imgTag = string.Format(fmt, "heres my document icon image", imgName);

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

One thought on “SharePoint: Mapping Document File Extensions to Document Images

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s