How to pass current document reference information to a page from jsx?

I have a backend page that will answer to requests from other pages(with ajax) and perform actions on those pages.

In my backend page I’d like to use this API to reference the document from which the backend page is queried:

#set($queryingDocumentReference   = $services.model.createDocumentReference(wikiName, spaceNamesList, DocumentName))

And not the other one $xwiki.getDocument('XWiki:Space.Document'). To support the pages that have dots in their names.

I checked out the JavaScript API page and found this:

require(['xwiki-meta'], function (xm) {
  xm.documentReference  // since 7.3M2, get the reference of the current document (as a DocumentReference object).
  xm.document           // get the current document (eg: Main.WebHome) -- deprecated since 7.3M2, use documentReference instead               // get the current wiki (eg: xwiki)            -- deprecated since 7.3M2, use documentReference instead              // get the current space (eg: Main)            -- deprecated since 7.3M2, use documentReference instead               // get the current page name (eg: WebHome)     -- deprecated since 7.3M2, use documentReference instead
  xm.version            // get the current document version (eg: 1.1)
  xm.restURL            // get the REST url of the current doc (eg: /xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome)
  xm.form_token         // get the current CSRF token that you should pass to your scripts to avoid CSRF attacks.
  xm.userReference      // since 10.4RC1 and 9.11.5, get the reference of the current user
  xm.isNew              // since 11.2RC1, define if the current document is new or not
  xm.locale             // since 12.3RC1, get the locale of the current document

And looks like extracting reference information from this javascript API would be extremely hard for the $services.model.createDocumentReference API.

For now I just used xm.documentReference on the front end and $xwiki.getDocument($request.documentReference) on the backend, but what happens when there are dots in the page names? Bugs?

Another solution I can think of is passing this reference information to the jsx myself from the rendering macro:

#set ($documentRef = { "wikiName" : "", "spaceNames" : [], "documentName" : "" })
#set ($documentRef.wikiName = $doc.documentReference.getWikiReference().getName())
#foreach($space in $doc.documentReference.getSpaceReferences())
#set($discard = $documentRef.spaceNames.add($space.getName()))
#set ($documentRef.documentName = $doc.documentReference.getName())
#set ($documentRefJson = $jsontool.serialize($documentRef))
#set ($discard = $xwiki.jsx.use($wikimacro.doc, {'documentRefJson' : $documentRefJson}))

This will pass the document where the jsx is being rendered in the json format and support dots in the page names, but this feels like way too much code for something like this, too.

And I also found out that I can’t directly do this in jsx, because $doc in jsx corresponds to where the jsx is included, not where it’s being rendered. And in this case this jsx is being included by the rendering macro, which is being used in other pages.

On the JavaScript side you can obtain the string representation of the document reference with:

const strDocRef = XWiki.Model.serialize(xm.documentReference);

See . You can then pass this string to the server side (e.g. as a request parameter / query string parameter), where you resolve the reference:

#set ($queryingDocumentReference   = $services.model.resolveDocument($request.documentReference))

Hope this helps,

1 Like