1

So I have a glassMapped item with an image.

[SitecoreType(TemplateId = TemplateIdConstants.NationalChain, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public interface IChain : IGlassBase
{
    [SitecoreField("Logo")]
    Image Logo { get; set; }
}

Witch I then try to set:

using (new SecurityDisabler())
using (var db = new SitecoreContext(Sitecore.Context.Database))
{
    var item = db.Create<IChain, IGlassBase>(parent, model.Name);
    item.Logo = new Image { MediaId = image.ID.ToGuid() };
    db.Save(item);
}

This does not work, so I'm looking for the correct way to do this.

Edit, I found one way that works..although I'm not happy with it. I added the following to the interface:

[SitecoreField("Logo")]
string LogoString { get; set; }

I then set the raw value like this:

item.LogoString = "<image mediaid=\"{" + image.ID.ToGuid() + "}\" />";

Is there any better way of doing this?

The following gives a strange result. Image is set while Logo is not set.

[SitecoreType(TemplateId = TemplateIdConstants.NationalChain, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public interface IChain : IGlassBase
{
  [SitecoreField("Logo")]
  Image Logo { get; set; }
  [SitecoreField("Image")]
  Image Image { get; set; }
}
  item.Image = new Image { MediaId = image.ID.ToGuid() };
  item.Logo = new Image { MediaId = image.ID.ToGuid() };

What am I missing?

2
  • Any exception? Or nothing happens?
    – Marek Musielak
    Apr 12, 2019 at 15:44
  • Nothing happens, other fields are saved ok, just not the image.
    – devzero
    Apr 12, 2019 at 15:47

1 Answer 1

1

Not sure with your GlassMapper version, but anyway check SecurityDisabler, SitecoreType attribute (provide error details to get answer faster).

Working solution with class (instead of interface)

Model:

[SitecoreType(TemplateId = "{[your template ID]}"]
public class ChildItem
{
    [SitecoreField("Image")]
    public virtual Image Image { get; set; }

    [SitecoreId]
    public virtual Guid Id { get; set; }

    [SitecoreInfo(SitecoreInfoType.Url)]
    public virtual string Url { get; set; }

    [SitecoreInfo(SitecoreInfoType.Name)]
    public virtual string Name { get; set; }
}

Saving:

 using (new SecurityDisabler())
 {
    using (var service = new SitecoreService("master"))
    {
        var childItem = new ChildItem
        {
          Name = "child";
          Image = new Image {MediaId = new Guid("{[your media ID]}")};
        };
        service.Create(parentItem, childItem);
    }
 }
11
  • Your code does exactly the same as mine, except you'r code wont work since we are using interfaces and not classes.
    – devzero
    Apr 12, 2019 at 15:49
  • Where are SecurityDisabler, SitecoreType attribute, SitecoreService in your code?
    – x3mxray
    Apr 12, 2019 at 15:52
  • I'm using SitecoreContext instead of Service, the other two I didn't put into the example code, also MediaId is a Guid not a text string
    – devzero
    Apr 12, 2019 at 15:57
  • Ive added disabler and type so the example is complete
    – devzero
    Apr 12, 2019 at 16:01
  • 1
    Does mapping work when you try to retrieve existing item image with name Logo with GetItem<>? Or if it works with name "Image", may be you have any other field "Logo"?
    – x3mxray
    Apr 12, 2019 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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