User macro to get space categories (plus more)
Asked by Randy Arrasmith:

Hello, I wrote this macro to retrieve all spaces and display some usage information like number of pages and number of users. I need to use this information to "charge" people internally for how much they use confluence so we can pay for our licenses year to year. Small teams pay less than big team. This macro is working so far, but some teams have organized in such a way they created multiple spaces. I went throught the site and added a category to every site to group them. How do I retrieve this category in a user macro? I have searched for two days and found nothing. It appears it is a label so I have searched on that and found very little and can't get it to work. (I also found some about space groups and have no idea what that is)

Once I get a push in the right direction on getting either the "category" or label (or space group), I will rewrite the macro to group by the label, list respective spaces, pages, and users.

```
## @noparams#set ( $allSpaces = $spaceManager.getAllSpaces() )Hello, $action.remoteUser.getFullName(). &lt;br /&gt;There are $userAccessor.countUsersWithConfluenceAccess() total Confluence users. &lt;br /&gt;Below is a list of all confluence spaces and number of users per space: &lt;br /&gt;&lt;table&gt;#foreach ($space in $allSpaces)  #if ($space.isGlobal() )    #set($userlist = [] )    #set($grouplist = [] )    &lt;tr&gt;    &lt;td&gt;#foreach ($label in $space.getLabels())             $label &lt;br /&gt;           #end   &lt;/td&gt;    &lt;td&gt;$space.getName()&lt;/td&gt;    &lt;td&gt;$spaceManager.findPageTotal($space)&lt;/td&gt;     #foreach ($permission in $space.getPermissions() )               #if ($permission.isUserPermission() &amp;&amp; !$userlist.contains($permission.getUserSubject().getFullName()) )                     #set ( $added = $userlist.add($permission.getUserSubject().getFullName()) )               #end               #if ($permission.isGroupPermission() &amp;&amp; !$grouplist.contains($permission.getGroup()) )                    #set( $added = $grouplist.add($permission.getGroup()) )                    #set( $groupmembers = $userAccessor.getMemberNamesAsList($userAccessor.getGroup($permission.getGroup() ) ) )                    #foreach ($member in $groupmembers)                      #if (!$userlist.contains($member))                         #set($added = $userlist.add($member))                      #end                    #end               #end      #end   &lt;td&gt;      $userlist.size()    #*      #foreach ($user in $userlist)        $user &lt;br&gt;      #end    *#   &lt;/td&gt;    &lt;td&gt; #foreach ($adminuser in $spaceManager.getSpaceAdmins($space))              $adminuser.getFullName() &lt;br&gt;            #end    &lt;/td&gt;   &lt;/tr&gt;  #end#end&lt;/table&gt;
```


Answers:
author: Tim
body: I suppose you want to get your hand on the SpaceLabelManager to retrieve the categories or space labels. I have not tried it out myself, but it should work like this in your user macro:

```
#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($spaceLabelManager=$containerContext.getComponent("spaceLabelManager"))#foreach ($space in $allSpaces)    #foreach ($label in $spaceLabelManager.getLabelsOnSpace($space))	$label.getName() &lt;br /&gt;    #end#end
```


author: Randy Arrasmith
body: Thank you, TtheB. That is the push-in-the-right-direction I needed. Getting the spacelabelmanager was a challenge for me (noob). I took your info and came up with the below as a standalone script that list all global spaces and the category label for each. The getLabelsOnSpace doesn't return the category label (they must be treated differently) and it turns out that get**Team**LabelsOnSpace is really the categories (since I didn't see a method to get categories I just tried the team one and it worked). The key for me, again, was getting to the spacelabelmanager. I will incorporate this into the larger script and group by the category and I found a script that list attachments sizes too so this should be a good "usage" script in the end.

```
## @noparams#set ( $allSpaces = $spaceManager.getAllSpaces() )#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($spaceLabelManager=$containerContext.getComponent("spaceLabelManager")) #foreach ($space in $allSpaces)    #if ($space.isGlobal() )      $space.getName() -       #foreach ($label in $spaceLabelManager.getTeamLabelsOnSpace($space.key))       $label.getName()      #end      &lt;br /&gt;  #end#end
```


Tags: confluence-administration

user-macro