Page URL of content item

July 13, 2024 1:45 AM

Is there a simple way to get the url of the parent page that a linked content item is linked to?

For example, I have ArticlePage and Article content types. The article content type is for articles I create in the Content Hub and then link to those on ArticlePage items in the tree. When I'm querying the Article items, I'd like to have the page they're linked (specifically the URL of that page) to use in my view. How would I go about this?


Environment

Answers

Use the Linking() content query extension.

It's used in the Kentico Community Portal project to do the exact thing you are asking about.

// Full query to retrieve entire content graph
var postsQuery = new ContentItemQueryBuilder().ForContentType(BlogPostPage.CONTENT_TYPE_NAME, queryParams =>
{
    _ = queryParams
        .ForWebsite(request.ChannelName)
                .Linking(nameof(BlogPostPage.BlogPostPageBlogPostContent), contentItemIDs)
        .WithLinkedItems(2);
});

In this example the first parameter passed to Linking() is the field name in your Article page content type that references the reusable content item. The second parameter is the Content Item IDs of the article resusable content items.

You could just retrieve the web pages and then glue them to the reusable content items if you already have access to them. But I find it's simpler to retrieve the whole content graph (.WithLinkedItems(2)) since this is all behind cache anyway.

Thanks Sean!

So now I'm getting a "No content type has been configured" Invalid Operation Exception when I use .WithLinkedItems(2)

Here is my code:

public async Task<IEnumerable<ArticlePage>> GetEditorsPicksAsync(ArticlePage page)
{
    var idsQuery = new ContentItemQueryBuilder().ForContentType(Article.CONTENT_TYPE_NAME, config => config.Columns(nameof(Article.SystemFields.ContentItemID)));

    var contentItemIDs = (await executor.GetResult(idsQuery, c => c.ContentItemID)).ToList();

    var query = new ContentItemQueryBuilder()
                .ForContentType(
                        ArticlePage.CONTENT_TYPE_NAME,
                        config => config
                        .ForWebsite("WebsiteName")
                        .TopN(4)
                        .Linking("ArticleContent", contentItemIDs)
						.WithLinkedItems(2)
                        ).InLanguage("en");

    IEnumerable<ArticlePage> editorsPicks = await executor.GetMappedWebPageResult<ArticlePage>(query);

    return editorsPicks;
}

Have you generated your content type classes recently? They need to have the RegisterContentTypeMapping attribute.

See the BlogPostPage.generated.cs file from the Community Portal project.

If you want to use GetMappedResult or GetMappedWebPageResult, your content type classes need this attribute to support the auto-mapping.

Also, whatever assembly your content type classes are in needs the AssemblyDiscoverable attribute so that the types can be discovered and the RegisterContentTypeMapping attribute can be processed.

To answer this question, you have to login first.