Need assistance in a user macro script
Asked by Aravind Koushik Gobburu:

Hi we have a user macro named "UPDATE HISTROY". This has been hadr coded for only particular space only. So i need guidance in this script so that it will be used for all spaces in confluence. Do kindly let me know if any body can help with this. Thanks in advance. Here is the script what i have

## @noparams

#set ( $descendantPages = $pageManager.getPage("NTGSDHSI","Project Status").getChildren() )

#set ( $compareDate = $action.dateFormatter.getCalendar() )

$compareDate.setTime($content.getCurrentDate())

$compareDate.add(6, -7)

\<table\>

\<tr\>

\<th\>Status Page\</th\>

\<th\>Last Modified By\</th\>

\<th\>Last Modified Date\</th\>

\<th\>Modified in last seven days?\</th\>

\</tr\>

#foreach ( $page in $descendantPages )

\<tr\>

\<td\>\<a href="$

{req.contextPath}

$childPage.urlPath"\> $page.title \</a\>\</td\>

\<td\> $page.lastModifier.name \</td\>

\<td\> $action.dateFormatter.format($page.lastModificationDate) \</td\>

\<td\>

#set ($pageDate = $action.dateFormatter.getCalendar())

$pageDate.setTime($page.getLastModificationDate())

#if ( $pageDate.before($compareDate) )

\<span class="aui-lozenge aui-lozenge-error"\>NO\</span\>

#else

\<span class="aui-lozenge aui-lozenge-success"\>YES\</span\>

#end

\</td\>

\</tr\>

#end

\</table\>


Answers:
author: Dominic Lagger
body: Ok, I think I found the solution. But be aware, this solution uses methods which are depricated in version 7.3. This means, they might work for longer (worked with 7.13), but atlassian can always delete this methods.

What I did:

* Get all spaces
* get all spacekeys
* iterate over all spaces to find pages with "Project Status"
* iterate over all descendants

Hope this is what you want

```
## Macro title: My Macro
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## This is an example macro
## @noparams

#set( $spaceManager = $action.getSpaceManager())
#set( $actualSpace = $spaceManager.getSpace($space.key))
#set ($spaces = $spaceManager.getAllSpaces())

#set( $allSpaceKeys=[] )
#foreach ($space in $spaces)
#set ($key = $space.getKey())
#set( $dummy = $allSpaceKeys.add($key) )
#end

#set( $descendantPages=[] )
#foreach ($spacekey in $allSpaceKeys)
#set ( $dumm = $descendantPages.add($pageManager.getPage($spacekey,"Project Status").getChildren() ))
#end


#set ( $compareDate = $action.dateFormatter.getCalendar() )

$compareDate.setTime($content.getCurrentDate())

$compareDate.add(6, -7)

<table>

<tr>

<th>Status Page</th>

<th>Last Modified By</th>

<th>Last Modified Date</th>

<th>Modified in last seven days?</th>

</tr>

#foreach ($descendants in $descendantPages)
#foreach ( $page in $descendants )
<tr>
<td><a href="$
{req.contextPath}$childPage.urlPath"> $page.title </a></td>

<td> $page.lastModifier.name </td>
<td> $action.dateFormatter.format($page.lastModificationDate) </td>
<td>

#set ($pageDate = $action.dateFormatter.getCalendar())
$pageDate.setTime($page.getLastModificationDate())

#if ( $pageDate.before($compareDate) )
<span class="aui-lozenge aui-lozenge-error">NO</span>
#else
<span class="aui-lozenge aui-lozenge-success">YES</span>
#end

</td>
</tr>
#end
#end

</table>
```


author: Dominic Lagger
body: Hi [@Aravind Koushik Gobburu](/t5/user/viewprofilepage/user-id/4224125)

On almost the first line, you go and get the page with the Spacekey NTGSDHSI and the title "Project Status". ([see documentation](https://docs.atlassian.com/ConfluenceServer/javadoc/7.2.0/com/atlassian/confluence/pages/PageManager.html#getPage-java.lang.String-java.lang.String-))

```
#set ( $descendantPages = $pageManager.getPage("NTGSDHSI","Project Status").getChildren())
```

So it will always get these pages, doesn't matter where you put your macro.

Regards, Dominic

comments: author: Aravind Koushik Gobburu
body: Hi [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) ,

I think you have wrongly understood my problem. Let me explain you clearly. This update history macro is hard coded for only particular space(like mentioned NTGSDHSI). Now we need to modify the code so that it works for all the spaces present in confluence. So final conclusion is that update history macro must be used for all spaces not only for a single space. And many thanks for the reply. Awaiting for your response.

Regards,

Aravind


author: Dominic Lagger
body: I understand you correctly, I just wanted to point out, why it's just working for that space. Because you hard code the key in your marco.

If you want to work it for all spaces, then perhaps this could help you.

```
#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $page = $pageManager.getPage($content.getId()) )
```

This give you the actual page, where the macro is sitting. From there, you should be able to do the rest.


author: Aravind Koushik Gobburu
body: Hi [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) ,

Really sorry for that i misunderstood you. Thanks for the code. I am testing it out. Will tell you once if it is an successful one or not. Thankyou so much for the code.

Thanks,

Aravind


author: Aravind Koushik Gobburu
body: Hi [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) ,

I tried this out but it's not working as expected.Its not giving us the update history of all the spaces or in particular space.Can you briefly modify the code what I have provided you and modify that to work for all spaces? If it's possible try that once.Or a big thanks for helping me out in this.

Regards,

Aravind


author: Dominic Lagger
body: Hey [@Aravind Koushik Gobburu](/t5/user/viewprofilepage/user-id/4224125)

I'm not sure what is your goal. What should this macro do?

And also, what do you mean by: "update history of all the spaces or in particular space". You can't have a space in a space. Do you mean pages?   

And last question, what history do you want to update? The whole user macro just display a table, and do not update anything, it never did.

Thanks and Regards, Dominic


author: Aravind Koushik Gobburu
body: Hi [@Dominic Lagger](/t5/user/viewprofilepage/user-id/601600) ,

When you see the code it gives us a table present in project NTGSDHI. This means that it is hardcoded for particular space only right. Now i want a modification to the code that works for the remaining spaces as well not for particular space NTGSDHI. Can we do this is what my question is.


Tags: confluence-server

server