Dynamic Alias Path GetDocumentName Macro

Joshua Adams asked on May 3, 2016 15:52

Can anyone tell me the best way to get the documentname of a document through a macro using just the alias path?

{% x = QueryString.GetValue("url") ; Documents[ToString(x)].DocumentName #%}{% x %}

The statement above will display the querystring value for the url that I have, but I can't seem to find the correct syntax to put the x variable in quotes and have it give me that documents name. The other item here is that the resource is actually a file, which I don't believe matters in this case, but please correct me if I am wrong.

Thanks in advance,

Correct Answer

Trevor Fayas answered on May 3, 2016 16:13

Here's an excerpt from our internal kentico wiki, should provide you what you need in the 2nd code snippet!

Here's a nifty way to take a page's NodeID and then to use the Macro Querying to get pieces of information from the page. Here's some syntax we used to grab the Node Alias:

{% RootDocument.AllChildren.Where("NodeID=" + QueryString.ID)[0].GetValue("NodeAliasPath") %}

NOTE: It is better, if possible, to use the Node Alias Path instead, as you can reference pages directly through that means. Plus the NodeID may change between staging and production sites so only use this if the NodeID is generated on the site. That format is:

{% Documents["/TheNodeAlias/Path"].GetValue("SomeValue") %}

Advanced Data: Sometimes when you are using the hierarchical repeaters, it doesn't return the full document data (ex. mobile menus and such). Use the Documents.WithAllData macro:

{% Documents.WithAllData[NodeAliasPath].DocumentMenuRedirectUrl %}

2 votesVote for this answer Unmark Correct answer

Recent Answers

Jan Hermann answered on May 3, 2016 16:14 (last edited on March 24, 2018 12:15)

The url query string has to contain the NodeAliasPath starting with the slash like "/Path/To/Some/Page" and without any extension and then you can use this:

{% Documents[QueryString.GetValue("url")].DocumentName %}

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on May 3, 2016 16:17 (last edited on March 24, 2018 12:15)

To piggyback off of Jan's, you can use the ? macro syntax to shorten your macro further:

{% Documents[url].DocumentName %}

1 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on May 3, 2016 16:33

Thanks guys, found out the problem. Looks like I am using the permanent url instead, What database field would contain that value, so I can just set a where clause such as Trevor suggested above

RootDocument.AllChildren.Where("NodePermanantURLField=" + QueryString.ID)

0 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on May 3, 2016 17:00 (last edited on March 24, 2018 12:15)

Update, ended up hacking it up a bit, and basically grab the nodeguid from the permanant url and use that with Trevor's query above. It works, and its not idea, because its based on that url being the same for all of the download documents, which thankfully they are in this case. Hope that helps someone in the future.

{% x = QueryString.GetValue("url"); y=x.Split("/"); z=y[2]; RootDocument.AllChildren.Where("NodeGUID='" + z + "'")[0].GetValue("DocumentName") %}

Also, thanks for the help everyone!

0 votesVote for this answer Mark as a Correct answer

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