alfy
alfy

Reputation: 3

How do I get the Kentico page permalinks using the REST API?

I'm writing a simple exporting tool for a client running Kentico 13 using its REST API. My goal is to ultimately import all the content into Wordpress.

I'm able to get a large JSON with information about all the objects by doing:

curl -u USERNAME:PASSWORD -X GET https://mykentico.site/xp-admin/rest/content/currentsite/en-us/all?format=json

Then I'm parsing every object in that file by querying individual AliasPaths, to get more detailed information about each object (title, category, content, etc.) like this for example:

curl -u USERNAME:PASSWORD -X GET https://mykentico.site/xp-admin/rest/content/currentsite/en-us/document/News-Overview/June-2024/Hello-World?format=json

However the JSON I'm getting back from that last request does not contain any "permalink", "canonical URL", or any other field (that I see) that I can use to deduce the actual frontend permalink of the (page) object. This is a problem, as the actual permalink for that last page is typically something like:

https://mykentico.site/newsletter/june-2024/hello-foobar-world

i.e. the permalink folders, as well as the actual page name, have different names than the AliasPath folders and document name.

Since I will need to map the old Kentico URLs with the new Wordpress URLs, I need to be able to query the REST API and record the permalink of each individual Kentico page.

Can someone please help? Thank you.

I tried perusing the Kentico REST API at length, trying to understand how I can get this individual page permalink information from the REST API. To no avail.

Upvotes: 0

Views: 59

Answers (2)

Amarnath behera
Amarnath behera

Reputation: 21

In Kentico 13, the "AliasPath" or "DocumentUrlPath" fields in the content tree often don't directly correspond to the final frontend URL (permalink) due to routing configurations, custom URL patterns, or rewrite rules that might be applied.

Unfortunately, the Kentico REST API may not provide the exact "frontend" URL in the direct response. However, you can try the following approaches to extract or deduce the correct URLs:

1. Check for the NodeAliasPath and DocumentUrlPath

  • If you're already using the REST API to pull data from Kentico, look for fields such as NodeAliasPath or DocumentUrlPath. These fields represent the path from the root of the site but may not exactly match the frontend URL.
  • For example, querying /rest/content/currentsite/en-us/document/News-Overview/June-2024/Hello-World should return these properties, but the final URL might depend on routing rules.

2. Use URL Redirection Rules

  • If there are custom URL redirection rules in Kentico, you could retrieve these by checking Kentico's URL redirection modules or settings.
  • In Kentico, these might be accessible from custom tables, page templates, or URL configurations that affect the permalinks.

3. Programmatic Resolution via Kentico API (Code-based)

If the REST API does not expose the canonical URL directly, you may need to:

  • Use Kentico’s custom code to extract or resolve the correct URL. If you have backend access to Kentico, you can build a custom endpoint or use a direct API call (not REST) to get the final URL.
  • For example, you can write a custom Kentico module using their internal API to resolve URLs:
    var page = DocumentHelper.GetDocuments()
        .Path("/News-Overview/June-2024/Hello-World")
        .OnSite("currentsite")
        .FirstObject();
    
    var url = URLHelper.GetAbsoluteUrl(DocumentURLProvider.GetUrl(page));
    

4. Use Kentico's URL Resolver Module

Kentico's built-in URL resolving module may provide the necessary URL if configured to expose friendly URLs.

5. Manual Mapping if No Other Option

Suppose none of the methods provide direct permalinks via the API. In that case, you may need to manually map the AliasPath to the correct permalink structure by mimicking the routing logic used in Kentico.

To summarize, check if the DocumentUrlPath field exists in the API response or consider writing a small module using the Kentico internal API to resolve canonical URLs. This would ensure you get the exact frontend URLs for WordPress migration.

Upvotes: 0

jurajo
jurajo

Reputation: 995

The links to the pages are generated dynamically on the front end side. So, these data are not stored anywhere in the DB. Then, it depends whether you are using custom or content tree based routing (https://docs.kentico.com/13/developing-websites/implementing-routing)

Upvotes: 0

Related Questions