The requirement is to have an image field that is not the standard one but rather a pulldown list of images from a specific folder. I can get this to work on the backend by using a droplist field or droplink field and pointing the Source item of the field to the folder in question, however, trying to then retrieve the actual image in code has proved problematic. I can't seem to cast the item to a mediaitem (it yields null), so I'm wondering if this scenario is typically handled in a different way or are there solutions I've just not been able to find?

  • 1
    Can you please share the code? Also if i understood properly, you want users to select images only from a specific container? – Hishaam Namooya Apr 10 '18 at 20:44

You can use either a droplink, droplist or even a droptree. However, you need to know how Sitecore stores the value of the selected image.


For droplink, if you check the raw values, you will see that the data is a Sitecore ID.


For droptree, if you check the raw values, you will see that the data is a Sitecore ID.


For droplist, if you check the raw values, you will see that the data is the name of the item selected.

So, I have tested on how you can retrieve the selected images and cast it as MediaItem.

I have use the Home item and updated the Sample Item template with the following 3 fields

  • Image Droplist

  • Image DropLink

  • Image Droptree

enter image description here

If you use the DropLink or DropTree, it is very simple as you just need to get the item by ID and cast it as a MediaItem. For Droplist, since the name of the item will be obtained, you will need to add the full path. Below is the code snippet on how to retrieve and cast the item as MediaItem for the 3 fields.

var database = Factory.GetDatabase("master");

var homeItem = database.GetItem(new ID("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"));

// If not all fields are being shown, uncomment below code
// homeItem.Fields.ReadAll();

var imageDroplist = homeItem.Fields["Image Droplist"].Value;

var imageDropLink = homeItem.Fields["Image DropLink"].Value;

var imageDropTree = homeItem.Fields["Image Droptree"].Value;

var imageDropListItem = (MediaItem)database.GetItem($"/sitecore/media library/Default Website/{imageDroplist}");

var imageDropLinkItem = (MediaItem)database.GetItem(new ID(imageDropLink));

var imageDropTreeItem = (MediaItem)database.GetItem(new ID(imageDropTree));

On a side note, I would tend to use the DropTree as field type because it will allow me to see the thumbnail of the image.

|improve this answer|||||
  • Thanks, however when trying to use the droptree code you provide above I get an error when casting specific to the new ID() - "System.Web.UI.Control.ID is a property but is used like a type. – Levi Wallach Apr 11 '18 at 12:39
  • AdditionallyI see you have "Image Droptree" etc. as field types in your screenshot but I'm not seeing these as options - do I need to create custom field types? – Levi Wallach Apr 11 '18 at 12:40
  • @LeviWallach those are not custom fields. It is found under the dropdown of when selecting the field type under section List Types and so on. new ID() is from the namespace Sitecore.Data and not from System. – Hishaam Namooya Apr 11 '18 at 12:45
  • @Hihaam Namooya, sorry, I was looking at your field labels! I made the change to the ID, modified it based on my existing code and it now works properly, thank you! – Levi Wallach Apr 11 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.