Search code examples
sitecoresitecore6glass-mapper

Getting an Internal Link with Glass.Mapper


I've got an Internal Link set up in Sitecore, and I'm trying to map the field using Glass.Mapper, but it just keeps coming back empty, and I'm not sure what I'm doing wrong.

The template in Sitecore is pretty simple: Screen capture of Sitecore template

The Source of the link is set to a folder that only allows content based on the 'System' template to be created.

In my code, I have an object set up:

namespace Playground.GlassObjects
{
    public partial class Status
    {
        public virtual string Description { get; set; }
        public virtual string StatusCode { get; set; }
        public virtual Glass.Mapper.Sc.Fields.Link System { get; set; }
    }
}

Which is being used basically like this:

public void DoStuff(Sitecore.Data.Items.Item item)
{
    var status = item.GlassCast<Status>();
    this.DoOtherStuff(status);
}

What I'm running into is glassObj.Description, and glassObj.StatusCode are being wired up exactly like I want/expect, but glassObj.System is not.

Screen capture of visual studio watch window

Can anyone tell me what I'm doing wrong here? I'm at a loss right now, with all the magic that's going on behind the scenes.


Solution

  • The Glass.Mapper.Sc.Fields.Link class is designed to work with the General Link field. The internal link field stores values as paths e.g /sitecore/content/home/events. This means it isn't compatible with the Link class.

    Instead you should map it to another class you have created.

    public partial class Status
    {
        public virtual string Description { get; set; }
        public virtual string StatusCode { get; set; }
        public virtual MySystem System { get; set; }
    }
    
    public class MySystem{
    
       public virtual string Url { get; set; }
       public virtual string MyField { get; set; }
    }