1

I have a template representing some rendering parameters. One of the fields on this template is a "general link" field which is supposed to link to an item in the Sitecore tree. I use the Synthesis object mapper framework.

The problem is that although I set a value in this field on my presentation details, the referenced item is null in my controller.

public class BrandController : Controller
{
    public ActionResult Index()
    {
        // IBrandParametersItem is my template
        var renderingParameters = RenderingContext.CurrentOrNull.Rendering.Parameters.As<IBrandParametersItem>();

        // BrandFolder is the general link field.
        // The BrandFolder property is of type IPathItemReferenceField
        // The brandFolder variable becomes null(!) although the field contains a link in the rendering parameters.
        var brandFolder = renderingParameters.BrandFolder.Target.As<IBrandFolderItem>();

        var brands = brandFolder.Children.Select(brand => brand.As<IBrandItem>()).ToList() ?? new List<IBrandItem>();

        return View(new BrandViewModel
        {
            Brands = brands
        });
    }
}

Looking at the Synthesis code, I have learned that BrandFolder.Target uses the path set in BrandFolder.TargetPath to resolve the item. When I debug, I can see that BrandFolder.TargetPath is set to something like

<link text=\"\" anchor=\"\" linktype=\"internal\" class=\"\" title=\"\" target=\"\"
querystring=\"\" id=\"{F9E446BB-1EF5-4B9D-A99E-446DF05CB208}\" />

So although it seems like the field value is correct(?), BrandFolder.Target still resolves to null. Any ideas?

1 Answer 1

1

It turned out that PathItemReferenceField was not the correct type for my general link field. The field had previously been of another type (internal link, I believe) and because of an incomplete Unicorn-sync the change to general link had not propagated to my Synthesis model in the code.

When I did a proper sync, the type of the field changed to HyperlinkField. This type "contains" the referred item in the TargetItem property, as opposed to the Target property on PathItemReferenceField.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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