Get fieldvalues from items in custom macro

Thomas Seyssens asked on June 9, 2016 14:17

Hello,

We're creating a custom breadcrumb function where we need field values from the items we want to display. Underneath you can see our code to receive the link & title:

Item is of type TreeNode. "<div class='pull-left item'><a href='" + item.RelativeURL + "'>" + item.GetValue(Constants.Title) + "</a></div>"; Our problem is as following: The GetValue function to receive the Title does not always answer correctly. In most cases it returns NULL.

Anyone has an idea what is going wrong?

Correct Answer

Dawid Jachnik answered on June 10, 2016 13:15

Yes, you can do it easly with DocumentQuery.

According to my previous example

 string parentName = "";
if (CurrentDocument.Parent != null)
{
var parent =    CMS.DocumentEngine.DocumentHelper.GetDocuments("namespace.classnameOfYourPageType")
.WhereEquals("DocumentID", CurrentDocument.Parent.DocumentID)
.InRelationWith(CurrentDocument.NodeGUID)
.FirstObject;
        if (parent != null)
            parentName = parent.GetValue<string>("TitleColumn", "");
}

this line is responsible for extra whee condition for relates pages - the parametr is the NodeGUID of the pages should be related .InRelationWith(CurrentDocument.NodeGUID)

Don't forget to mark as the correct one my answer :)

Thanks

Dawid

1 votesVote for this answer Unmark Correct answer

Recent Answers


Dawid Jachnik answered on June 9, 2016 15:18

Hello,

if you want to display this in static text or similar web part you can simply use item.TitleColumn

How do you retrive this document? Make sure that your data source containc .WithAllData parameter (https://docs.kentico.com/display/K9/Using+transformations+in+macro+expressions#Usingtransformationsinmacroexpressions-Displayingpagesfromthecontenttree)

0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 9, 2016 15:23

Hello,

Since this custom macro is called in the static text or similar, the code it has to run is placed in a .cs file. There we receive a TreeNode object, from which we can navigate.

There is no option on this object called WithAllData & the shortcut .TitleColumn gives build errors..

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 9, 2016 15:32

So you don't use K# right?

How you retrive the TreeNode from the database?

if you use

DocumentHelper.GetDocuments()

it doesn't contains your custom page type specific column and you should use

DocumentHelper.GetDocuments("codename.pageTypeClassName")    

Previusly example was for K# and TitleColumn you should replace to your title column name

0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 9, 2016 15:41

We receive the TreeNode with the customMacro call e.g. GetBreadCrumb(CurrentDocument)

We receive in the function some parameters. The parameter we receive is a TreeNode, since it is the document sund from the macro.

From that TreeNode we can go to the parents, children, everything. The only strange thing that is happening is, that the current item, which we receive from the call, can do the GetValue() call, which fails with the parent nodes of this item.

The parent nodes of the item are all of type TreeNode.

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 9, 2016 15:48

I don't understand why you use your custom method to recive the documents.

BTW. CurrentDocument is TreeNode type, so what the method GetBreadCrumb do ?

Why don't you use repeater to achive this?

0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 9, 2016 15:56

To explain why we use the custom method, our client wants a custom breadcrumb, that contains items of their choosing. Items that are not necessarely a parent(single or multiple levels) in the content tree.

e.g. home > Destinations > Asia > Thailand

In this case, Asia is no parent of Thailand, and Destinations is no parent of Asia.

We call the GetBreadCrumb macro in a static text web part, which will execute a custom function, that will construct a ready-made breadcrumb, with all items we attach to it.

The CurrentDocument sund from the static-text web part does contain the fields and renders the fields. It is its parent or child that does not.

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 10, 2016 08:23

Ok, now I understand it.

Your macro GetBreadCrumb returns List of TreeNodes right?

Does the Destinations, Asia and Thailand got the same named field Constants.Title ?

0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 10, 2016 10:09

Yes, it returns TreeNodes. And yes, they all inherit from the same page, where the Title field is defined. On all items the Title field is filled in.

But for one or another reason, only the current field can be read.. Almost seems like not the full item is received when asking the parent node.

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on June 10, 2016 11:58 (last edited on June 10, 2016 11:58)

When you call for parent document though CurrentDocument.Parent in your method it won't contain your custom field. You need again get the Parent document from the database for example

 string parentName = "";
 if (CurrentDocument.Parent != null)
 {
    var parent = CMS.DocumentEngine.DocumentHelper.GetDocuments("namespace.classnameOfYourPageType").WhereEquals("DocumentID", CurrentDocument.Parent.DocumentID).FirstObject;
            if (parent != null)
                parentName = parent.GetValue<string>("TitleColumn", "");
 }
0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 10, 2016 13:07

Hello Dawid!

Indeed! I can reach the fields now! Thanks a lot. Now i only need to find out how i can get a related page (with Pages Form control).

Country relates to Continent with pages selector, which makes it clear to me a relation is made between the 2 tables.

Is there an easy selector to get the related items?

0 votesVote for this answer Mark as a Correct answer

Thomas Seyssens answered on June 10, 2016 13:23

Thanks a lot Dawid! It works!

0 votesVote for this answer Mark as a Correct answer

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