Problem with mapping after API request

I have a problem with mapping a Page to PageModel in a page search after API request.

When I do the same type of request from the web site the mapping works, but after the API request i get pageModel as null.

var page = query.FirstOrDefault();
if(page != null){
	var pageModel = page.MapTo<PageModel>();
}

I have debugged and the page object looks the same i both requests.

Any suggestion?

Litium version: 7.1.0

1 Like

Is the page published for the channel?

Yes, its published on all the channels.

Do I need to specify which channel im using when mapping in any way?

No, I don’t think so. I know from earlier that pageModel can be null if the page isn’t published for the current channel or if the current user doesn’t have access to it.

Okey, I tried to use SecurityContextService.ActAsSystem() for permissions also but no difference.
Thanks for suggestions anyway.

Can you share some additional information? How is the request made?

I’m using postman trying to call my Api request. I’m sending som info in the body which is used to search for pages.

    private static PageModel GetPage(Result result, Guid websiteSystemId, string pageTemplateName)
    {
        var fieldTemplateService = IoC.Resolve<FieldTemplateService>();
        var pageTemplate = fieldTemplateService.Get<PageFieldTemplate>(pageTemplateName);
        using (var query = IoC.Resolve<DataService>().CreateQuery<Page>())
        {
            query.Filter(f => f.TemplateSystemId("eq", pageTemplate.SystemId).Status(ContentStatus.Published).WebsiteSystemId(websiteSystemId));
            if (pageTemplateName == PageTemplateNameConstants.Result)
            {
                query.Filter(f => f.Field(PageFieldNameConstants.TypeId, "eq", result.TypeID));
                query.Filter(f => f.Field(PageFieldNameConstants.Code, "eq", result.Code.ToString()));

                var page = query.FirstOrDefault();
                if (page != null)
                {
                    var pageModel = page.MapTo<PageModel>();

                }
	}
	...
    }
}

For external API calls you need to include the “litium-request-context” header.

Some more discussion here:
https://forum.litium.com/t/moving-clientcontext-to-external-call/

Thank you, the second link helped me understand how it worked so I could solve it.

OK, great!

Just a side-note, but a small reminder of this key aspect of using the data service:

Query information with data service is bypassing the caches. You should not use it without a cache if the result is to be used on public pages for visitors.

Source

1 Like