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
NilsN
April 25, 2019, 7:15am
2
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?
NilsN
April 25, 2019, 8:02am
5
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.
NilsN
April 25, 2019, 8:41am
7
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>();
}
}
...
}
}
NilsN
April 25, 2019, 1:07pm
9
For external API calls you need to include the âlitium-request-contextâ header.
You need to send the âlitium-request-contextâ in the request header so the server can detect the Request context. The value of âlitium-request-contextâ header should be an instance of SiteSettingViewModel
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.
NilsN
April 26, 2019, 7:11am
11
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