Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
In case anyone ever stumbles across this:
The solution looks like this:
[ServiceConfiguration(typeof(IContentApiModelFilter), Lifecycle = ServiceInstanceScope.Singleton)]
public class CustomContentApiModelFilter : ContentApiModelFilter<ContentApiModel>
{
private readonly IContentLoader contentLoader;
private readonly IContentTypeRepository contentTypeRepository;
private const string CONTENT_TYPE_GUID = "ContentTypeGuid";
public CustomContentApiModelFilter(IContentLoader contentLoader, IContentTypeRepository contentTypeRepository)
{
this.contentLoader = contentLoader;
this.contentTypeRepository = contentTypeRepository;
}
public override void Filter(ContentApiModel contentApiModel, ConverterContext converterContext)
{
// apply any filtering as needed... etc
contentApiModel.Properties[CONTENT_TYPE_GUID] = Guid.Empty.ToString("N");
if (contentApiModel.ContentLink.Id != null && contentApiModel.ContentLink.Id.HasValue)
{
var content = contentLoader.Get<IContent>(new ContentReference(contentApiModel.ContentLink.Id.Value));
var contentType = content.ContentTypeID;
var type = contentTypeRepository.Load(contentType);
if (type != null && type.GUID != Guid.Empty)
{
contentApiModel.Properties[CONTENT_TYPE_GUID] = type.GUID.ToString("N");
}
}
}
}
I am trying to add a property to all IContent objects coming out of the Content Delivery API (v3).
I'm trying with a ContentFilter like so:
[ServiceConfiguration(typeof(IContentFilter), Lifecycle = ServiceInstanceScope.Singleton)] public class CustomContentFilter : ContentFilter<IContent> { public override void Filter(IContent content, ConverterContext converterContext) { content.Property.Add("TESTNAME", new PropertyString("TESTVALUE")); } }
However, this doesn't seem to change the results coming out of the content delivery API.
What would be the recommended approach to inject a new property to all instances of IContent?
PS- the property I am trying to inject is actually the Content Type identifier, as I feel that this will be much more reliable for the consuming application. The content types array that exposes the C# model type feels really fragile to me, as a refactor of code could break that.