User macro to get space categories (plus more)

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(). <br />
There are $userAccessor.countUsersWithConfluenceAccess() total Confluence users. <br />
Below is a list of all confluence spaces and number of users per space: <br />

<table>
#foreach ($space in $allSpaces)
  #if ($space.isGlobal() )
    #set($userlist = [] )
    #set($grouplist = [] )
    <tr>
    <td>#foreach ($label in $space.getLabels())
             $label <br />
           #end
   </td>
    <td>$space.getName()</td>
    <td>$spaceManager.findPageTotal($space)</td>
     #foreach ($permission in $space.getPermissions() )
               #if ($permission.isUserPermission() && !$userlist.contains($permission.getUserSubject().getFullName()) )
                     #set ( $added = $userlist.add($permission.getUserSubject().getFullName()) )
               #end
               #if ($permission.isGroupPermission() && !$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
   <td>
      $userlist.size()
    #*
      #foreach ($user in $userlist)
        $user <br>
      #end
    *#
   </td>
    <td> #foreach ($adminuser in $spaceManager.getSpaceAdmins($space))
              $adminuser.getFullName() <br>
            #end
    </td>
   </tr>
  #end
#end
</table>

2 answers

1 accepted

This widget could not be displayed.

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() <br />
    #end
#end
This widget could not be displayed.

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 getTeamLabelsOnSpace 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
      <br />
  #end
#end

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

134 views 1 4
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you