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.