Question

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.

Was it helpful?

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; }
}

OTHER TIPS

Fast forward to 2022 Internal Link field seems to be working with Glassmapper without any extra effort. All you have to do is add Internal Link case to GlassGenerator.tt file on the project where you will generate the template.

enter image description here

This will ensure your model will have Link field like this:

[SitecoreField(FieldId = "{D2CF138A-0A1C-4766-B250-F56E9458B624}")]
Link InternalLinkField{ get; set; }

It will have some info populated and most of the other properties will be null. The ones that will help you are:

  1. Url (full path to the internal link item)
  2. TargetId (ID of the internal link item)

Here are the available properties: enter image description here

There is an alternative to that, you can get the link from fields like this:

yourGlassItem.Item.Fields["InternalLinkFieldName"]

You will get the entire Internal link Item. You can use Value or InheritedValue property to get the path of the linked item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top