7

Is there a way to update a list's icon? I was looking to use SPList.ImageUrl, but this is read only.

The problem is that I've updated a list definition's icon; unfortunately there several list instances based on this definition already being used out there in the real world.

Is there some workaround I can use to resolve this?

5
  • 2
    How about extreme workaround? Go in content database, table AllList, find your list and change tp_ImageUrl field. Jan 31, 2012 at 15:09
  • 1
    @VedranRasol: I sincerely doubt any of our customers would be happy with us editing their content databases, and thereby possibly compromising any future support from Microsoft... Although it may be a solution for non-ISVs. :)
    – Stu Pegg
    Jan 31, 2012 at 15:15
  • I know. That is why I used comment instead of answer and used words like extreme and workaround :) Jan 31, 2012 at 15:18
  • Extreme -> horrifying? ;)
    – Stu Pegg
    Jan 31, 2012 at 15:20
  • ... and utterly unsupported. if MS finds out that you are changing content directly in the database they will not support the app at all
    – Dave Wise
    Jan 31, 2012 at 15:30

2 Answers 2

11

Nothing in SharePoint is simple and this question proves it. I spend last hour or so on this hoping to find some remotely simple solution. (See update for SharePoint 2013 below)

ImageUrl is not a 'native' SPList property. I did some decompiling and refactoring only to find out that ImageUrl is one of the many list properties retrieved by SPListCollection class. I didn't figure out any way of updating it. I can write 2 pages on my findings but it would be useless.

So what are the other workarounds:

1. Simple image replacement

Hopefully in your original list definition you are using custom image (eg. deployed to images folder by feature). You just overwrite it with new image (using the same name). Manually, by feature, by code...

2. Using javascript

Do it on client side. Just add some javascript to your masterpage and do the switch when desired page is loaded. Here is simple example:

var listdeftype = '10000'; // List template Type from List definition
var newimgloc = '/_layouts/images/mycoollistimage.png';

if(window.location.href.indexOf("viewlsts.aspx") > -1) {
    var atags = document.getElementsByTagName('a');
    for(i=0;i<atags.length;i++){
        if(atags[i].getAttribute('id') == 'viewlist' + listdeftype ){
            var img = atags[i].getElementsByTagName("img");
            if(img[0] != null) {
                img[0].src = newimgloc ;
            }
        }
    }
}

This script replaces all list icons on All Site Content page based on your custom list definition.

3. Migration

Create new instance of list based on new list definition and then copy all items from original list.

4. Database update (not recommended)

Go in content database, table AllList, find your lists and change tp_ImageUrl field. In SP content database there is even stored procedure proc_UpdateListSettings that does the list updates. This procedure accepts @ImageUrl as parameter. This procedure is called when you do SPList.Update(). Why is ImageUrl exception (because you can set most of the list properties) is beyond my knowledge.

DISCLAIMER!

My testing environment is SP2010. Not a single workaround I listed is really useful. I deliberately didn't include all limitations and possible problems to make things more readable. I am only posting my answer because I spend some time on this issue and I want to share my findings. My only real contribution here is yet another¸proof that word simple and SharePoint cannot be used in same sentence (oh look, I did it).


EDIT: This all changed with SharePoint 2013 as Microsoft introduced a setter for SPList.ImageUrl. The MSDN description still doesn't tell you you can set the value, but you can! :-)

The following code works perfectly (look Ma, I even did it in PowerShell):

$web = Get-SPWeb https://<yourweb>
$list = $web.Lists["<ListName>"]
$list.ImageUrl = "/_layouts/15/images/itann.png?rev=23"
$list.Update()

This will update the list image to the announcements default image. Thanks Microsoft for giving us a setter.

1
0

I you want to use your own icons you can change the url for specified png file. The is below

SPList asset = web.Lists["Site Assets"];

SPList list = web.Lists.TryGetList("Tasks");
if (liste != null)
{
    using (FileStream fs = new FileStream(string.Format(@"C:\Assets\l{0}.png", list.Name), FileMode.OpenOrCreate))
    {
        asset.RootFolder.Files.Add(string.Format("l{0}.png", list.Name), fs, true);
    }
    list.ImageUrl = string.Format("/SiteAssets/l{0}.png", listeadi)
    liste.Update();
}

If you look in attention you will a toke at the begining of png file. the is "l" letter (capitalize : L because it looks like capital "i") For example you should give ltasks.png.

1
  • This will work in SP2013, as noted in the previous answer. The issue I mention in the question is that your penultimate line (list.ImageUrl = ...) is invalid in the previous versions (the versions current when the question was asked).
    – Stu Pegg
    Dec 19, 2014 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.