Add property to all IContent?

Vote:
 

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.

#270695
Jan 27, 2022 15:25
Vote:
 

In case anyone ever stumbles across this:

  1. I wish you the best of luck.
  2. The solution was to use an IContentApiModelFilter instead and append the property just before the content is being sent to the client.

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");
                }
            }
        }
    }
    
#270701
Edited, Jan 27, 2022 20:57
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.