0

I'm working with a Sitecore MVC website and in a controller, I'm trying to programmatically populate the Src property of the Glass.Mapper.Sc.Fields.Image class. However, it was annoyingly declared with only a public getter and an internal setter:

public string Src { get; internal set; }

Note also that there is only a parameterless constructor in this class. There must therefore be another way to set this value internally, but after an exhaustive search online, I couldn't find anything that worked.

Note that in the controller, I have all of the values that are required to fully populate the Image object, including the Sitecore item ID. I also have access to the parent Sitecore item of type Sitecore.Data.Fields.ImageField that contains the data for the Image object.

I've tried the following, but it returns an empty Image instance.

SitecoreService sitecoreService = new SitecoreService(database);
Image image = sitecoreService.GetItem<Image>(imageField.InnerField.ID.Guid);

And when I tried this, it creates a new Image object with its dimension and Alt fields correctly set, but not the Src property, for some reason.

Image img = sitecoreService.GetItem<Image>(imageField.MediaItem.ID.Guid);
var parent = imageField.MediaItem.Parent;

Please let me know how I can programmatically set the Src property of this Image class, or convert the Sitecore field into an Image instance.


UPDATE >>>

I followed @DrazenJanjicek's advice and managed to map the parent item using the sitecoreService.GetItem method, which successfully populated all of the Image properties for me.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I guess that main problem in call sitecoreService.GetItem is that Image is not class model that contains fields definitions. It is field definition, but not an item definition. You can create your own class Image with image item template fields and work with it as with another items. – Anton Apr 26 '16 at 11:46

2 Answers2

1

I think creating the proper Glass type from your Sitecore item makes more sense and simply use the Image property. There is no simple API for creating only the Image type. You can do that but in the end Glass will create the outer type first.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    Initially, I thought that your answer was not very helpful, as it didn't actually provide any help with how I might follow your suggestion. However, I'm fairly resourceful and so have found a way to create the parent object using similar code and you were right... it did successfully create the `Image` objects for me. Thanks. – Sheridan Apr 26 '16 at 11:47
0

This code works for me:

var image = new Image() {MediaId = mediaItem.ID.ToGuid()};

You should set MediaId for an image.

Anton
  • 9,682
  • 11
  • 38
  • 68
  • Thanks for your response, but I have tried that and all I get is an `Image` instance with only the `MediaId` property set. How can that possibly populate its other properties? It has no reference to them, as we're only passing a `Guid` to the instance. Can you please explain further, bearing in mind my inexperience with Sitecore? For example, what item should I pass to the `GetItem` method? I have been using the `ImageField.MediaItem` item. – Sheridan Apr 26 '16 at 11:03
  • @Sheridan, sorry first line is odd, I remove it. Code that I posted is used for saving Image to Glass model. Then I do next: someitem.Image = image; MasterService.Save(someItem); And next item when I'll ge someItem via glass service I'll get an image field filled correctly. – Anton Apr 26 '16 at 11:43