Check if item is a MediaItem

You 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.)

3 comments

  1. 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

  2. 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

  3. (please ignore previous comment)

    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

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