November Happy Hour will be moved to Thursday December 5th.

Implementing IPropertyModelConverter for EpiServer ContentDelivery API

Vote:
 

Hello team,

Can someone help me with below issue?

I have a content delivery API and in response I have a field (for one of my pages) ContentReference for Images. I need to add some extension to the Image URL that comes back in JSON response (http:\\xyz.com..\..image.jpg to http:\\xyz.com..\..image.jpg?v=xxxx&height=&width)

I am trying to implement CustomPropertyModelConverter but am stuck on how to identify if the content reference is an image and how to update the ModelType.

Also am not sure if this is the correct way to do it? Can someone provide an example on how can I achieve this.  

#222708
May 11, 2020 16:31
Vote:
 

If it's just one specific property that you want to add the query string parameters to, it might be better to just add it as a new property on the converted model it in the ExtendedContentModelMapper.cs (assuming you've used the Music Festival site as a template).

You could do it like this (in the TransformContent method, after contentModel is transformed:

if(content is YourPageType page)
{
    var url = UrlResolver.Current.GetUrl(page.YourImageProperty);
    var imageWithParams = $"{url}?v=xxxx&height=&width";
    contentModel.Properties.Add("imagewithparams", imageWithParams);
}
#222856
May 12, 2020 12:06
Vote:
 

Thanks, This solved my current issue. If I have more properties where I need modification, would I still use this method or do by implementing IpropertyModelConverter.

I am going to accept your answer.

I am also trying to strip out fields from ContentApiModel by overriding ContentResultService as mentioned in documentation (https://world.episerver.com/documentation/developer-guides/content-delivery-api/how-to-customize-data-returned-to-clients/) but it doesn't get triggered. 

Do you happen to have any reference on how to get this done as well.?

#222859
May 12, 2020 13:48
Vote:
 

We use IPropertyModelConverter when we need to tell the ContentModelMapper how to convert a custom property, so as long as what you're doing is to modify values of properties etc. you could still use this method. 

As for your other question, did you register your custom service as per the documentation in the DependencyResolverInitalization-module?

#222861
May 12, 2020 14:35
Vote:
 

Yes, I have registered in the dependency resolver. Have done all the steps as per the documentation. I also found a thread on similar issue 

https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2019/1/contentresultservice-is-executed-instead-of-the-injected-one/ 

I also see quite a few overrides in DefaultContentConverter but honestly other than a couple, not really finding documentation on what those methods can really do.

#222864
May 12, 2020 15:25
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.