Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set MediaItem on ImageField

Currently mediaItem.Src is throwing an obsolete warning and states using the MediaItem property instead. The mediaItem.MediaItem has no setter though. Is this warning really just to get people to use something like:

var image = new Sitecore.Data.Items.MediaItem(imageField.MediaItem);
Sitecore.Resources.Media.MediaManager.GetMediaUrl(image);

Is .Src (and related field properties) still the recommended way to actually set the field's value?

like image 381
al3xnull Avatar asked Dec 03 '25 15:12

al3xnull


2 Answers

I believe you'd set the media ID instead of the media item property.

like image 143
Kam Figy Avatar answered Dec 06 '25 13:12

Kam Figy


I would refer to the Content API Cookbook (PDF link) under section 4.1.7

Some sample code from it:

home.Editing.BeginEdit();
imageField.Clear();
imageField.Src = Sitecore.Resources.Media.MediaManager.GetMediaUrl(sampleMedia);
imageField.MediaID = sampleMedia.ID;
imageField.MediaPath = sampleMedia.MediaPath;
...
like image 41
Mark Ursino Avatar answered Dec 06 '25 13:12

Mark Ursino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!