How to create an image using the glass.mapper?

1,534 views
Skip to first unread message

Pierre Clement

unread,
Jun 14, 2013, 3:48:00 PM6/14/13
to glass...@googlegroups.com
Hello,

I have a class that has the following property :

[SitecoreField(FieldName = "Image")]
public virtual Glass.Mapper.Sc.Fields.Image ThumbnailImage { getset; }

I am trying to create a duplicate of that image into another folder in Sitecore. I try to create the duplicate using this syntax :

this.masterService.Create(this.imagesRepositorynewImage);

where masterService is SitecoreContext("master"), imagesRepository is the Sitecore item where i want to put the duplicate and the newImage is the new image as the name tells you.

Everytime, i get a NullReference object and the image cannot be added.

When i try to get the image using the sitecore glass base class like this :

[SitecoreField(FieldName = "Image"FieldType = SitecoreFieldType.Image)]
public virtual GlassItem ThumbnailImage { getset; }

or like this :
[SitecoreField(FieldName = "Image")]
public virtual GlassItem ThumbnailImage { getset; }

the returned object is null.
The GlassItem contains the basic sitecore properties and it works fine with other items except for images so far.

Anyone can help me fix this problem?
Thank you

Pierre Clement

unread,
Jun 14, 2013, 4:41:07 PM6/14/13
to glass...@googlegroups.com
I forgot to mention that i am using Glass.Mapper v3.

Michael Edwards

unread,
Jun 15, 2013, 3:31:19 AM6/15/13
to Pierre Clement, glass...@googlegroups.com
Pierre 

Please see this blog post:

Mike


On 14 June 2013 21:41, Pierre Clement <pierre....@nexio.com> wrote:
I forgot to mention that i am using Glass.Mapper v3.

--
You received this message because you are subscribed to the Google Groups "Glass.Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glassmapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Pierre Clement

unread,
Jun 17, 2013, 4:05:03 PM6/17/13
to glass...@googlegroups.com, Pierre Clement, mikeed...@googlemail.com
Hello Mike,

I have changed my ThumbnailImage object from GlassItem to a MediaImage with all the properties that you put in the blog post. I was able to retrieve the image item like this :
var image = service.GetItem<MediaItem>("/sitecore/media library/Images/ImageCopy/Koala");
Unfortunatly it doesn't work in my current solution. I have a template that contains an image and more properties. I have a class that contains all the properties and the MediaImage field like this :

[SitecoreField(FieldName = "Image")]
public virtual MediaImage ThumbnailImage { getset; }

When i retrieve the item through the same service, that field is empty but it's not empty in sitecore.

Can you tell me if you can reproduce this behavior?

Can you help me fix this?
Thanks.


By the way, you made some silly mistakes in your blog post.
var image = service.GetItem>MediaItem<("/sitecore/media library/Images/ImageCopy/Koala");
You have written the >MediaItem< instead of <MediaItem>. You have done the same mistake on both call to GetItem.



Michael Edwards

unread,
Jun 18, 2013, 2:15:14 AM6/18/13
to Pierre Clement, glass...@googlegroups.com

Can you send me more of the code. Are you targeting the master or Web database? Is everything published?

Cheers

Pierre Clement

unread,
Jun 18, 2013, 10:06:01 AM6/18/13
to glass...@googlegroups.com, Pierre Clement, mikeed...@googlemail.com
[SitecoreType(TemplateId = "{A7DDE686-7A68-4E69-B9CC-9829EA3725E7}"AutoMap = true)]
public class ModelBase : ContentBase
   // This is a multilist field.
   [SitecoreField(FieldName = "Brands")]
   public virtual IEnumerable<BrandsBrands { getset; }
}

// This is the Brands class which i had trouble with the MediaImage.
[SitecoreType(TemplateId = "{6FACEFD5-ED2B-4C99-8E25-E908C570C19F}"AutoMap = true)]
public class Brands : ContentBase
{
   [SitecoreField(FieldName = "GammeID")]
   public virtual string GammeId { getset; }
 
   // This is the image field with jpeg extension which uses the unversioned jpeg template.
   [SitecoreField(FieldName = "Thumbnail Image")]
   public virtual MediaImage ThumbnailImage { getset; }
}

// This is the ContentBase that is used for all sitecore items
[SitecoreType(AutoMap = true)]
    public abstract class ContentBase
    {
        /// <summary>
        /// The Name of the Data section of the item.
        /// </summary>
        [SitecoreField(FieldName = "Name")]
        public virtual string DataName { getset; }
 
        [SitecoreField]
        public virtual string Title { getset; }
 
        [SitecoreId]
        public virtual Guid Id { getset; }
 
        /// <summary>
        /// The Name of the Sitecore item.
        /// </summary>
        [SitecoreInfo(SitecoreInfoType.Name)]
        public virtual string ItemName { getset; }
 
        [SitecoreInfo(SitecoreInfoType.DisplayName)]
        public virtual string DisplayName { getset; }
 
        [SitecoreInfo(SitecoreInfoType.Language)]
        public virtual Language Language { getset; }
 
        [SitecoreInfo(SitecoreInfoType.Url)]
        public virtual string Url { getset; }
 
        [SitecoreInfo(SitecoreInfoType.Version)]
        public virtual int Version { getset; }
 
        [SitecoreField]
        public Image Icon { getset; }
 
        [SitecoreInfo(SitecoreInfoType.TemplateId)]
        public virtual Guid TemplateId { getset; }
}

// And lastly, this is the mediaImage class. This template id is the unversioned jpeg
[SitecoreType(AutoMap = trueTemplateId = "{DAF085E8-602E-43A6-8299-038FF171349F}")]
public class MediaImage : ContentBase
{
   private Guid templateId;
 
   public MediaImage()
   {
      this.templateId = new Guid("{DAF085E8-602E-43A6-8299-038FF171349F}");
   }
 
   [SitecoreInfo(SitecoreInfoType.TemplateId)]
   public override Guid TemplateId 
   { 
       get { return this.templateId; }
       set { this.templateId = value; }
   }
 
   public virtual Stream Blob { getset; }
 
   public virtual string Extension { getset; }
 
   public virtual int Size { getset; }
}

So basicly, when i retrieve the ModelBase from sitecore, the brands list is not empty, I got the GammeId in each brands but the ThumbnailImage is null everytime.
I do this line to retrieve the ModelBase.

this.MasterService = new SitecoreService("master");
var model = this.MasterService.GetItem<ModelBase>(itemId);

Any ideas where the problem might be?
Reply all
Reply to author
Forward
0 new messages