How to check Preview Mode or Edit mode in API?

I've few API, when I try to get data in preview page, it's always return live data,

I've debug, the function to get preview mode always return live mode, how I can get correct state here, do we have a function support it?


Environment

  • Xperience by Kentico version: 31.0.0

  • .NET version: 8

Answers

Suggestions:

  1. Use the Content Retriever. It automatically applies the request context (preview/live) to the content query and will retrieve unpublished content when viewing a page in the Page Builder or Preview tabs in the admin UI (based on the request URL). It will automatically switch to only retrieve published items when requests are made on the live website. You don't need to use any special APIs or configure the querying based on the request mode.
  2. Use the ContentItemQueryBuilder, which is a low-level API. It requires you to manually pass the preview context state to the query to retrieve unpublished content. You can use IWebsiteChannelContext.IsPreview for this.

Hi Sean,

Thanks for your information, the preview data work on first load, but I've a REST Api call with js after that, the data work get is not correct, I've checked and saw it's can not get the correct mode in Preview mode, do you have any solution for this?

Thanks

To response this discussion, you have to login first.