Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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
Community showcase
Published in Confluence

Confluence Mythbusters: Does Atlassian even use Confluence?

Hi, Confluence collaborators! As part of #Confluence-Collaboratory month, we’ve created a very special Mythsbusters segment, where we're dive into an interesting myth and uncover the truth behind i...

1,747 views 8 32
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you