Linking not work property
If I create a content item (A) with a field in which I can insert other content items (B), let's say 10, when in ContentItemQueryBuilder.ForContentType(A) I use Linking and pass 10 ids content items (B), contentQueryExecutor will return me 10 times content item (A). Is this how it should work?
Environment
- Xperience by Kentico version: [30.3.1]
- .NET version: [8]
- Private cloud (Azure/AWS/Virtual machine)]
- Link to relevant Xperience by Kentico documentation
Answers
I recommend sharing some actual code if you want people to be able to help you effectively 😊.
Yes of course
public async Task<IEnumerable<Category>> GetCategoriesTest(string lang, IReadOnlyCollection<int> articles, CancellationToken token)
{
var contentsQuery = new ContentItemQueryBuilder()
.ForContentType(Category.CONTENT_TYPE_NAME, queryParams =>
{
_ = queryParams
.Linking(nameof(Category.Articles), articles)
//.WithLinkedItems(2)
;
}).InLanguage(lang);
var queryOptions = new ContentQueryExecutionOptions()
{
IncludeSecuredItems = true,
ForPreview = true
};
return (await contentQueryExecutor.GetMappedResult<Category>(contentsQuery, queryOptions, token));
}
Let's assume the structure looks like this
C1-A1
C1-A2
C1-A3
C2-A1
C2-A4
When I execute this query with parameters where in articles I pass A1,A2,A3 in response I will get C1,C1,C2.
If I use Linking without WithLinkedItems Category.Articles will be empty, when I use with WithLinkedItems Category.Articles all Articles that are linked to both C1 and C2 will be returned, not only those passed in Linking
When you map to Category, the complete Category is mapped including all referenced articles, not only those you have included in your query. I think this is correct.
Maybe you could add additional logic on the response to filter it to your needs.
Or use an own model mapping logic.
The Linking() method behaves correctly in this case I believe, since the method limits the query to content items that link content items in the field specified by the first parameter (which in your case is Category.Articles). The curious thing is that it retrieves content items that link content items from the given collection for each item in the collection, which means that it will retrieve the same content item of the Category content type for each item in the collection since the item of the Category content type likely links all of those items. That is the reason why you got 10 times content item of type A in your original hypothetical scenario.
Here is a little example:
// Let's say we have only one article page with the title "X",
// which happens to link 8 products with IDs 1-8.
IEnumerable<int> productsCollection = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var builder = new ContentItemQueryBuilder();
// Retrieves items of the 'project.ArticlePage' content type
builder.ForContentType("project.ArticlePage", subqueryParameters =>
{
// Retrieves all content items of 'project.ArticlePage' type that reference any
// of the items in 'productsCollection' from their 'RelatedProducts' field
subqueryParameters.Linking("RelatedProducts", productsCollection);
});
// The result would be 8 times the page with title "X"
You can also try retrieving items from the opposite side of the linking relationship using LinkedFrom().
The documentation for the Linking method and others is available here: https://docs.kentico.com/developers-and-admins/api/content-item-api/reference-content-item-query#linking
Note that I haven't tried any thorough testing besides a quick query in the DancingGoat sample website provided by Xperience.
To answer this question, you have to login first.