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

User macro to get space categories (plus more)

Randy Arrasmith August 12, 2014

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

1 vote
Answer accepted
Tim
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 13, 2014

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
1 vote
Randy Arrasmith August 13, 2014

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
TAGS
AUG Leaders

Atlassian Community Events