TreeNode.GetRelatedDocuments() - Getting null error

Albert Zaballero asked on March 18, 2022 05:22

For a custom page type we are creating, which includes some standard text fields needed for page functionality, I also added a page type field that uses form control "Related Pages" in order to use related pages to be displayed/listed on the parent page in the front-end MVC app. The following documentation was used for reference:

We're using Kentico SP 12 and I realize the above link is for an older version. But, as far as I know it's still supported. However, when I use the generated page code:

public IEnumerable<TreeNode> RelatedContent
        return mInstance.GetRelatedDocuments("RelatedContent");

This results in a null reference exception. When debugging, the mInstance variable is not null. So that likely means there's an internal error that unfortunately is not exposed. I found this post which almost exactly matches the issue we're facing:

The screenshots in that post for the error as well as screenshots of the CMS fields are virtually the same as for us. I tried the answer David te Kloese posted, but it didn't work.

I found another post that offered a solution:

I haven't tried it yet. But, looking at the code it doesn't look like it's using generated code which I believe is the preferred approach. I also understand there is another technique for related pages as documented here:

This seems to be another viable approach, but I like the other option better since it offers a nicer UX for admins/editors. Does anyone have any guidance on this? What are we missing?

Correct Answer

Juraj Ondrus answered on March 18, 2022 10:01

You can use also the version 12 documentation - just at the top of the page use the version switcher. Anyway, in this case the docs are basically the same and I think I see where the confusion is coming from.
There are two types of relationships in Kentico: named and ad-hoc relationship.

Named relationship - the pages are being added on the page's Properties -> Related pages tab where you can select the relationship name (defined in the Relationship names app).
Ad-hoc relationship - this is the Pages field added to the page type, available on the Form/Content tab. This relationship does not have any name, just GUID.

And now, the confusion comes from the GetRelatedDocuments method parameter, 'fieldname' - but the method's signature says: "Retrieves a query that selects documents related to the current document with the relationship name given by specified field.". So, the field name is meant a field, which holds the name of the named relationship. But from your description I assume you are using ad-hoc relationship. In this case, you need to use the code mentioned in the other thread or also try using the .InRelationWith parametrization for the document query.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Albert Zaballero answered on April 7, 2022 00:43

Following up in case it helps anyone else.

Thanks Juraj Ondrus, I marked your reply as the answer. Especially, letting me know about the .InRelationWith() method.

Here's a bit more detail:

The Ad-hoc relationship Juraj described is indeed the approached we implemented per the documentation here:

I tried experimenting with that mInstance.GetRelatedDocuments("RelatedContent"); method. But, I could never get past that null exception.

The final solution that worked was the code example in the last post found here:

It succeeded in both fetching the related documents. But, also returned them in the correct order by using the RelationshipInfoProvider.ApplyRelationshipOrderData() method.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.