Check if item is a MediaItem
SitecoreYou can check if an item is a media item by using:
item.Paths.IsMediaItem
It checks if ‘media library’ is part of the item’s path.
I had a multilist of items that could be either regular content items or media items, and needed to loop through and render a link for each one. Because you use different methods to render a MediaItem link (MediaManager.GetMediaUrl()
) and regular item link (LinkManager.GetImageUrl()
), I needed to know what type the item was.
Comments (imported from wordpress)
Mike Reynolds
March 11, 2013 at 00:48
Hi Martina,
Sitecore.Data.ItemPath — items.Paths is an instance of this class — has another similar property for content items: IsContentItem. This helps calling code ascertain if the item is a descendant of /sitecore/content/.
Mike
Thomas D
August 23, 2013 at 08:31
Hi,
The property IsMediaItem just checks if the path starts with “/sitecore/media library/”. So a media folder within the media library is also considered a MediaItem. I handled this by also checking on TemplateID:
item.Paths.IsMediaItem && item.TemplateID != TemplateIDs.MediaFolder