SPList Image and SPFile Icon

If you ever wanted to create a WinForms app for SharePoint that displays or interacts with lists, document libraries and files, then taking advantage of the SPList.ImageUrl and SPFile.IconUrl would help improve the user experience for your app.

Below is an app that displays all the webs, lists, document libraries and files in a selected site collection.

We can improve this a little bit by adding icons (taken from TEMPLATE\IMAGES frolder on a SharePiont installation) for some of the nodes, as follows:

However, this is not good enough since we’ll have all lists with the same icon, all doc libs with the same icon and so on… Here is the same app taking advantage of the list image url and file icon url.

For the list or document library, this is done by using the SPList.ImageUrl attribute. Here is howthe tree node was added:

TreeNode listNode = parentNode.Nodes.Add(list.Title);
listNode.SelectedImageIndex = listNode.ImageIndex = GetImage(GetImageFullPath(list.ImageUrl));

For the file, this is done by using the SPFile.IconUrl attribute. Here is howthe tree node was added:

TreeNode fileNode = parentNode.Nodes.Add(file.Name);
fileNode.SelectedImageIndex = fileNode.ImageIndex = GetImage(GetImageFullPath(file.IconUrl));

The GetImageFullPath method simply gets the full path of the image:

/// <summary>
/// Gets the SharePoint full path from the relative path
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
private static string GetImageFullPath(string relativePath)
    //  Get image name
    string imageName = Path.GetFileName(relativePath);

    //  Get SharePoint IMAGES folder path
    string fullPath = SPUtility.GetGenericSetupPath(@"TEMPLATE\IMAGES");

    return Path.Combine(fullPath, imageName);

Given that now we have the full file system path to the image, all we have to do now is create an image from the file path then add it to the ImageList of the TreeView. The GetImage() method below does the job.

/// <summary>
/// Gets the image of the sp element from specified url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private int GetImage(string url)
    //  Get image index from the Image List
    int imageIndex = spImageList.Images.IndexOfKey(url);

    //  Image is not in the list, so add it
    if (imageIndex == -1)
        //  Get the image from specified path
        Image image = Image.FromFile(url);

        //  Add the image to the image list
        spImageList.Images.Add(url, image);

        //  Get its index
        imageIndex = spImageList.Images.Count - 1;

    return imageIndex;

Here is how the final app looks like. Click on the image below to download the src and exe.

About these ads
  1. Woah! I’m really digging the template/theme
    of this blog. It’s straight-forward, yet beneficial. A lot of times it’s hard to get that
    “best symmetry” between superb usability and visual appeal.
    I must say you’ve done a amazing job with this. Additionally, the blog loads super
    quick for me on Safari. Superb Blog!

  2. Je peux vߋus dire quee c’est clairement un bonheur
    dе vesnir suг cе blog

  3. Though a clarifying face cleaner works wonderful to minimize all traces of oil and give you a tidy feeling, in case you begin experiencing terribly itchy skin or red spots quickly stop the use of that particular facial cleanser.

    There are upscale shops that offer good quality Argan oils and are
    priced at a premium. When the make-up is left unaltered, oils manage to mix
    well with the natural structure of the human body, offering very desirable

  4. It’ll be worthwhile for the prospect to find the accreditation.
    Hairstyles in waves, curls, poker straight that smaller profitable
    thinning hair frizzy hair these days. Children and grandchildren can be shown the photographs of the wedding ceremony.

  5. I don’t even understand how I ended up here, but I
    thought this post was once good. I do not recognize who you might be but definitely you are going to
    a well-known blogger if you are not already. Cheers!

  6. On ѵa vous dire que сe n’est guèгe inexact …

  1. August 6th, 2014
    Trackback from : telepon satelit murah

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


Get every new post delivered to your Inbox.

%d bloggers like this: