Send content API response in another path

Vote:
 

For a headless site, if I want to get data for a navigation block I can call the content api and get the full json for it:

/api/episerver/v3.0/content/123

This returns around 3k (formatted) json lines.

I instead want to make it independant from the ID. I have created a custom controller in the following path: /api/client/nav

I call a custom converter:

public override ContentApiModel Convert(IContent content, ConverterContext contentMappingContext)
{
    var baseModel = base.Convert(content, contentMappingContext);
    var model = new HeaderBlockApiModel(baseModel);
    return model;
}

Which I can invoke

var headerBlockValue = headerBlockConverter.Convert(header as IContent, contentMappingContext);

The caveat is that, since I'm not in content api, I have to manually create a ContentMappingContext.

public ConverterContext GetDefaultExpandableContext()
{
    var options = new ContentApiOptions
    {
        ExpandedBehavior = ExpandedLanguageBehavior.RequestedLanguage,
        EnablePreviewMode = true,
        ForceAbsolute = true,
        IncludeEmptyContentProperties = true,
        IncludeSiteHosts = false,
        RichTextFormat = RichTextFormat.Structured,
        FlattenPropertyModel = false,
        EnablePreviewFeatures = true,
        IncludeMetadataPropertiesPreview = true
    };

    var converterContext = new ConverterContext(ContentReference.StartPage, Thread.CurrentThread.CurrentCulture, options, ContextMode.Default, "*", "*", false);
    return converterContext;
}

This works and gets me the block. However, the problem is it does not apply any filters or auto expand for the properties. So we get 600 lines of (formatted) json. None of the navigation is expanded.

Here the documentation for auto expand for content area: https://docs.developers.optimizely.com/content-management-system/v1.5.0-content-delivery-api/docs/customizing-conversion-from-icontent-to-contentapimodel

Is there a simpler way to achive this? If not, we can just fix the IDs on the FE side but it would ideally be generic.

#328248
Aug 28, 2024 18:00
Vote:
 

For anyone interested, add controllers as services in your startup when calling AddMvc:

This allows you to inject EPiServer.ContentApi.Cms.Controllers.Internal.ContentController in your own controller.

Then you can get the converted object in your custom controller as follows:

var header = contentService.GetHeader();

var queryParameters = new ContentQueryParameters
{
    References = $"{header.ContentLink.ID}"
};

var result = contentController.QueryContent(languages, queryParameters);

Some attributes are not respecting camel case, but it works otherwise.

This is probably not official / supported since it's calling internal classes, etc.

#328448
Aug 29, 2024 21:02
* 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.