Access the icon for an item

Version: 10.4

You can use the Sitecore.Data.Items.Item.Appearance.Icon property to access the icon for an item. If the icon contains a themed image, you can use the Sitecore.Resources.Images.GetThemedImageSource method to convert the relative path to a full path. For example, to determine the full path of the icon for the /Sitecore/Content/Home item in the Master database:

RequestResponse
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

string icon = home.Appearance.Icon;

if (icon.StartsWith("~"))
{
    icon = Sitecore.StringUtil.EnsurePrefix('/', icon);
}

else if (!(icon.StartsWith("/" ) && icon.Contains(":")))
{
    icon = Sitecore.Resources.Images.GetThemedImageSource(icon);
}

You can use the Sitecore.Data.Items.Item.Appearance.Icon property to set the icon for an item. For example, to set the icon for the /Sitecore/Content/Home item in the Master database to the themed image network/16x16/home.png:

RequestResponse
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

Sitecore.Data.Items.Item home = master.GetItem("/sitecore/content/home");

home.Editing.BeginEdit();

home.Appearance.Icon = "network/16x16/home.png";

home.Editing.EndEdit();

Do you have some feedback for us?

If you have suggestions for improving this article,