Confluence User Macro - Execute search query
Asked by Michael Ruderer:

Hello everybody,

is it possible to perform a search query in the background within a confluence user macro?

I want to display the found results in a bullet list at the confluence page. I have found some classes within the confluence SDK but i dont know how to get them working within velocity.

Example classe which could perform a search:

* [ConfluenceSearchProvider](https://docs.atlassian.com/confluence/5.9.9/com/atlassian/sal/confluence/search/ConfluenceSearchProvider.html "class in com.atlassian.sal.confluence.search")
* [ContentSearch](https://docs.atlassian.com/confluence/5.9.9/com/atlassian/confluence/search/v2/ContentSearch.html "class in com.atlassian.confluence.search.v2")
* [CQLSearchService](https://docs.atlassian.com/confluence/5.9.9/com/atlassian/confluence/api/service/search/CQLSearchService.html "interface in com.atlassian.confluence.api.service.search")

Have you got any ideas?

Thanks in advance!

Michael


Answers:
author: Stephen Deutsch
body: Instead of trying to mess with all that, why not just piggyback on top of one of the existing macros?

This should work for generating a CQL query:

```
## @noparams$action.getHelper().renderConfluenceMacro('{contentbylabel:showLabels=false|showSpace=false|cql=type = "page" and <rest of cql>}')
```

This displays more or less a bulleted list (with a page icon instead of bullets. If you want to modify the results, just assign the results to a variable and directly modify the text with the text manipulation methods available to velocity (for example, to not show anything when there are no results instead of the no labels found panel).

comments: author: Michael Ruderer
body: Hello Stephen,

thanks for your reply!

That?s a great idea and is working fine.

One problem: I am searching for a name which can be found within an attachment. Content By Label is returning then something like this:

"Page Link" \> "Link to attachment"

I have tried with string functions to just return the "Page Link" but this is not working fine. I have tried .split() and substring in combination with indexOf without any success. With the .split() function i only get returned an address of the string object:

![2017-07-10 09_18_47-Link auf OrgChart Test - Michael Ruderer - ONE - confluence.png](https://community.atlassian.com/t5/image/serverpage/image-id/2876i69F27F4BD31C4A52/image-size/large?v=v2&px=999 "2017-07-10 09_18_47-Link auf OrgChart Test - Michael Ruderer - ONE - confluence.png")

This is normally an array and i don?t know how to get the value within velocitiy not the object address...

Have you got any ideas?

Best Regards,

Michael


author: Stephen Deutsch
body: Hi Michael,

Actually, what you get returned is a list of strings. Unfortunately velocity (at least the version that is included in Confluence) doesn't have very good list functions, so you're pretty much limited to #foreach ($listItem in $list). Here is something fairly simple that should work:

```
## @noparams#set ( $htmlString = $action.getHelper().renderConfluenceMacro('{contentbylabel:showLabels=false|showSpace=false|cql=type = "attachment"}') )<ul>#foreach ( $htmlTag in $htmlString.split(' <') )  #set ( $htmlTag = '<' + $htmlTag )  #if ( $htmlTag.contains('/download/attachments/') )    <li>$htmlTag</li>
  #end#end</ul>
```


author: Michael Ruderer
body: Hi Stephen,

thanks that?s exactly what i needed!

Thank you very much!

Best Regards,

Michael