Wednesday, September 23, 2015

Sitecore get media item from ID

When dealing with fields which are of the ImageField type is is trivial to get the media item (image) that is referenced in that field. However when using a Treelist field, which allows users to select images, you are given a delimited string of item IDs of the selected image(s).
var item = Sitecore.Context.Database.GetItem(mediaItemID);
if (item.Paths.IsMediaItem)
  var mediaItem = new MediaItem(item);
To get the MediaItem, simply get it as a normal item (to ensure it exists and is actually a media item) and then parse it as media item.

No comments:

Post a Comment