2

Using native Sitecore ways, the following can be achieved by doing:

var pageItem = Sitecore.Context.Item;
var trackingField = new TrackingField(pageItem.Fields["__Tracking"]);

The question is following - how do you achieve same via glass mapper?

I've got following in my items interface, but that doesn't do the trick.

[SitecoreField("__Tracking")]
ITrackingItem TrackingCasted { get; set; }

The goal is to be able to access associated campaigns to the item.

As I was not able to do it via Glass Mapper, I've achieved access to associated campaigns via

var tracking = new Sitecore.Analytics.Data.TrackingField(MYITEM.Fields[Sitecore.Analytics.AnalyticsIds.TrackingField]);
tracking.Campaigns...

2 Answers 2

1

__Tracking is an advance field type for analytics. It stores the value in it as XML format. You can try by assigning the profile card on an item and see it's raw value. when you run the code -

var pageItem = Sitecore.Context.Item;
var trackingField = new TrackingField(pageItem.Fields["__Tracking"]);

basically, it is initializing and invoking TrackingField class by calling a parameter constructor and its required input is Field type.

Now when you run the code -

[SitecoreField("__Tracking")]
ITrackingItem TrackingCasted { get; set; }

It has two things to point - First, that it will return the value, not the field, second the return type should be a Glass model or supported .Net type for glass - http://glass.lu/mapper/documentation/Mapping-Fields

Since you are using an advance field type and by assuming your return type is correct, you will again need a Field type not the value for the tracking.

1

The easiest way to do what you want would be to add the raw Sitecore item as a property to your Glass model and then for that specific field, use the Sitecore item's property. This would make that property read-only.

public class YourModel {
    [SitecoreItem]
    public virtual Item Item { get; set; }

    public IEnumerable<string> CampaignIds => new TrackingField(Item.Fields["__Tracking"]).CampaignIds;
}

The more complex alternative would be to create a new Glass field type and field mapper and register them. That would allow you to simply add a property with that type to your model and be done with it. I might do up an example of that if I find time.

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.