How do I get the RelativeUrl from a NodeGUID?

Joshua Myers asked on October 1, 2019 22:38

I have a NodeGUID and I would like to print the RelativeUrl of that page within a link. The key is that I'm working within an HTML transformation, so this has to be done within a macro.

It works with a hard-coded NodeGUID, but I can't figure out how to make it dynamic based on a field from the page type itself.

This Works {% Documents.WithAllData.Where("NodeGUID = '97ace408-d74a-4673-82b8-f8a8a6547c41'").Transform("{# RelativeUrl #}") #%}

This Doesn't {% Documents.WithAllData.Where("NodeGUID = '{% ContinuingEdPage %}'").Transform("{# RelativeUrl #}") #%}

This Doesn't Either {% Documents.WithAllData.Where("NodeGUID = '" + ContinuingEdPage + "'").Transform("{# RelativeUrl #}") #%}

Recent Answers


Peter Mogilnitski answered on October 2, 2019 01:10 (last edited on December 10, 2019 02:31)

A couple things. You need to add culture to your query. Node can have multiple documents (one per each language), so no need for transformation - nodeGuid and culture will identify a unique document. Secondly Where returns InfoObjectCollection - use FirstItem instead. You do not need WithAllData if you are looking for common document properties like RelativeURL (WithAllData - might create an excessive SQL query):

{% Documents.Where("NodeGUID = '" + CurrentDocument.NodeGUID + "' and documentCulture='" + CurrentDocument.DocumentCulture + "'").FirstItem.RelativeURL|(identity)GlobalAdministrator%} to check if your are getting it correctly.

0 votesVote for this answer Mark as a Correct answer

Joshua Myers answered on October 2, 2019 16:05 (last edited on December 10, 2019 02:31)

Ok. This one seems to be working best! {% ResolveUrl(Documents.Where("NodeGUID = '" + ContinuingEdPage + "'").FirstItem.RelativeUrl) |(identity)GlobalAdministrator%}">Click here</a>

Within a macro that is part of a Static HTML webpart, is there a "document.write()" type of command that we can use to concatenate this value into another string?

Thanks in advance!

0 votesVote for this answer Mark as a Correct answer

Joshua Myers answered on October 2, 2019 16:33

UPDATE: I have found a solution, but it feels like quite the hack...
If I embed a 'Static HTML' widget inside the 'Static HTML' webpart, it doesn't replace all the quotes with the HTML encoded equivalent.

Is there a better way?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 2, 2019 21:11 (last edited on October 2, 2019 21:20)

it is hard to say without knowing the context. Static Text/Static HTML or LinkButton web part will work. The problem with quotes could be related to the fact that the system replaces single quote characters (') in the macro result with two single quotes (''). if you add {% |(handlesqlinjection)false%} at the end of the macro it will probably solve your issue.

{% ResolveUrl(Documents.Where("NodeGUID = '" + ContinuingEdPage + "'").FirstItem.RelativeUrl) |(handlesqlinjection)false@%}

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 2, 2019 21:44

P.S. You might as well call .AbsoluteUrl instead .RelativeUrl and avoid ResolveUrl

0 votesVote for this answer Mark as a Correct answer

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