Get URL from page with special chars in page-title within a user macro

Michael Rosenberger December 17, 2020

Hi,

we use the following code to get the url auf a page an create a link to it:

## @param Page:title=Page Name|type=confluence-content|required=true|desc=Select a target page


#set ($page = $pageManager.getPage($spaceKey, $paramPage))
#set ($pageUrl = $req.contextPath + "/pages/viewpage.action?pageId=" + $page.id)

This works as expected in most cases. But if the title of the page contains any special character (&,+, ...) no page is found and no pageId gets returned.

I have found an issue, that can be related:
https://jira.atlassian.com/browse/CONFSERVER-11285

Is there a way to get the url of a page with special-chars in title?

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 22, 2020

Try this. It takes into account pages in other spaces and pages with a ":" in the title. I've not tested every possible special character but it should work with most.

## @param Page:title=Page Name|type=confluence-content|required=true|desc=Select a target page

#if( $paramPage && $paramPage != "" )
    ##########################
    ## Find the page        ##
    ##########################
    #set( $parts = $paramPage.split(":") )
    #set( $i = 0 )
    #set( $len = 0 )
    #set( $key = "" )
    #set( $name = "" )

    ##Having trouble finding out the length/size of $parts ... brute force it
    #foreach( $part in $parts )
        #set( $len = $len + 1 )
    #end
    
    #if ( $len == 1 )
        #set( $key = $content.spaceKey )
        #set( $name = $paramPage )
    #else
        #foreach( $part in $parts )
            #if( $i == 0 )
              #set( $key = $part )
           #else
              #if( $i == 1 )
                #set( $name = $part )
              #else
                #set( $name = $name + ":" + $part )
              #end
           #end
           #set( $i = $i + 1 )
        #end
    #end

    #set( $name = $name.replace("&", "&") )
    #set( $name = $name.replace(""", '"') )

    #set( $page = $pageManager.getPage($key, $name) )
    #set( $pageUrl = $req.contextPath + "/pages/viewpage.action?pageId=" + $page.id )
#end
Michael Rosenberger February 5, 2021

Thank you, that helps. I needed this one, especially:

    #set( $name = $name.replace("&", "&") )
    #set( $name = $name.replace(""", '"')

 

Like Vladimir Vakhlov likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.4.6
TAGS
AUG Leaders

Atlassian Community Events