Map Monoco Link List field type with Synthesis

Synthesizing

Map strongly typed Link List field type with Synthesis when generating objects…

Versions used: Sitecore 8.1 rev. 160519 (8.1 Update-3)

This post is an addendum to my previous post about Monoco Link List field type in Sitecore 8.

I love using object mappers. On a previous project I was using synthesis and Link List field; and we wanted to be able to map our slick link list when generating synthesis models. I’ll show how to do this here. Source code here

NB: As mentioned in the previous post, this is an old code done in a rush, and might not be up to a good code standard, but it’s a good start and does the job.

The first thing we have to do is copy Fields\LinkListField.cs class (I’ll call it LinkListFieldSynthesis.cs in this post), extract an interface and add a new constructor required by Synthesis for lazy loading.

The interface looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public interface ILinkListFieldSynthesis
{
IEnumerable<Link> Links { get; }
string Root { get; set; }
XmlDocument Xml { get; }
Field InnerField { get; }
string Value { get; set; }
string GetAttribute(string name);
void SetAttribute(string name, string value);
List<WebEditButton> GetWebEditButtons();
void Relink(ItemLink item, Item newLink);
void RemoveLink(ItemLink itemLink);
void UpdateLink(ItemLink itemLink);
void ValidateLinks(LinksValidationResult result);
}

Then, you add a new constructor to the class that looks like this:

1
2
3
4
public LinkListFieldSynthesis(Synthesis.FieldTypes.LazyField lazyField, string indexValue)
: base(lazyField.Value, "links")
{
}

And finally, we tell synthesis about this new field type and how to map it by adding the following to file Synthesis.config

1
<map field="LinkList" type="Monoco.CMS.Fields.LinkListFieldSynthesis, Monoco.CMS" interface="Monoco.CMS.Fields.ILinkListFieldSynthesis, Monoco.CMS" />

That’s it. Compile your code and generate your synthesis models which you can then use like

1
2
3
4
5
6
foreach (var link in Model.MyProperty.Links)
{
<li>
<a href="@link.Url" target="@link.Target" alt="@link.Title">@link.Text</a>
</li>
}

====================

Source code: Download here


Please let me know what you think and/or if you can spot any errors.
/eom

Share