Get RelatedDocuments custom fields in Text/XML Transformation

Ryan Kilgore asked on July 20, 2022 00:46

I'm working on creating a transformation to display some related documents' information, but I'm running into a small issue. In this transformation, I'd like to display a grid of thumbnails pertaining to a "Related Resources" field I created (which is a data type of "Pages" and I grabbed the GUID of the field). I'm using a for loop to run through the field and applying a transformation to the object:

{% foreach (relatedResource in CurrentDocument.RelatedDocuments["namespace.CodeName_FieldGUID"].WithAllData) {
    ApplyTransformation(relatedResource, relatedResource.ClassName + ".Thumbnail")
} %}

(albeit with the correct GUID information)

The loop is correctly grabbing the pages I'd expect and the transformation is displaying some of the related documents' fields like DisplayName and RelativeURL, but all of my custom fields are null. They don't appear to be included in the RelatedDocuments object.

Is there an appropriate way for me to grab all of the data and custom fields from each related document in the loop? Both the transformation that has the foreach loop and the transformation I'm applying to the related documents are Text / XML transformations.

I would appreciate any insight!

Recent Answers

Brenden Kehren answered on July 20, 2022 03:58

The related document object does not have those page type fields in it. You'd have to make another query to get those values.

0 votesVote for this answer Mark as a Correct answer

Ryan Kilgore answered on July 20, 2022 07:09 (last edited on July 20, 2022 07:13)

Thanks Brenden!

I did at one point try to use:

{% foreach (relatedResource in CurrentDocument.RelatedDocuments["namespace.CodeName_FieldGUID"].WithAllData) {
    ApplyTransformation(Documents.Where("DocumentGUID='"+relatedResource.DocumentGUID+"'").FirstItem, relatedResource.ClassName + ".Thumbnail")
} %}

But the Documents.Where("DocumentGUID='"+relatedResource.DocumentGUID+"'").FirstItem portion just returns null, even though the relatedResource.DocumentGUID appears to be the correct value in some other output testing I did. Is this the wrong kind of document query to get the document object?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on July 20, 2022 22:53

Try this macro. Define the path, then ensure you use .AllChildren then use your where condition.

Documents["/"].AllChildren.Where("DocumentGUID = 'fc212579-408b-43db-9ac9-9e8eb0bce961'").FirstItem

This works when I test it in the Macro editor (System > Macros > Console)

0 votesVote for this answer Mark as a Correct answer

Ryan Kilgore answered on July 21, 2022 00:33

Hmm. So that macro works for me in the Macro editor as well, but not on the page itself. The Macro Debug tool also says it returned null.

The page it should be returning is checked in, published, and has no security restrictions. Are there other attributes to a page that would prevent it from working with a Documents query, but function just fine in the Macro editor?

0 votesVote for this answer Mark as a Correct answer

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