Show a Space List within a page using Category label

What I'm attempting to do is assign Space Categories to all my spaces, then generate lists on different pages to list spaces that have the corresponding category assigned.

I understand there is a Spaces List macro that will allow me to provide a View Spaces with Category dropdown where users can select the category they want to view by, but I'd like to avoid user input and instead provide a list using the category label.

2 answers

1 accepted

This widget could not be displayed.

I recently wrote a simple user macro that lists spaces by category:

## @param Label:title=Space Category|type=string|required=true|desc=The category you want to list spaces from.

## try to get space category
#set ( $spaceLabel = $action.labelManager.getLabel( "team:${paramLabel}" ) )

## check if space category exists
#if ( $!spaceLabel )
    ## try to get spaces by category
    #set ( $spaces = $action.labelManager.getSpacesWithLabel( $spaceLabel ) )
    <ul>
    #foreach ( $space in $spaces )
        ## check if current user can view space
        #if ( $permissionHelper.canView( $action.remoteUser, $space ) )
            <li><a href="$!space.urlPath">$!space.name</a></li>
        #end
    #end
    </ul>
#else
    <div class="aui-message warning">
        <p class="title">
            <span class="aui-icon icon-warning">Warning</span>
            <strong>Space category not found</strong>
        </p>
        <p>Couldn't find space category <strong>$paramLabel</strong>!</p>
    </div>
#end

Hope this helps

Great work Remo...exactly what I was looking for!

Me too! Perfect... Thank you!

This is great!

Is there a way to exclude archived spaces from the list?

Awesome … I'm trying to additionally sort the list alphabetically before output. Unfortunately Velocity SortTool seems not to be available within user macros and installing the required Velocity libraries is not an option at the moment. Anyone having another way of sorting the space list?

You can check if a space is archived with: {code} $space.isArchived() {code} The full version of the macro excluding archived spaces would be: {code} ## @param Label:title=Space Category|type=string|required=true|desc=The category you want to list spaces from. ## try to get space category #set ( $spaceLabel = $action.labelManager.getLabel( "team:${paramLabel}" ) ) ## check if space category exists #if ( $!spaceLabel ) ## try to get spaces by category #set ( $spaces = $action.labelManager.getSpacesWithLabel( $spaceLabel ) ) <ul> #foreach ( $space in $spaces ) ## check if current user can view space and space is not archived #if ( $permissionHelper.canView( $action.remoteUser, $space ) && !$space.isArchived() ) <li><a href="$!space.urlPath">$!space.name</a></li> #end #end </ul> #else <div class="aui-message warning"> <p class="title"> <span class="aui-icon icon-warning">Warning</span> <strong>Space category not found</strong> </p> <p>Couldn't find space category <strong>$paramLabel</strong>!</p> </div> #end {code}

Perfect. Thank you. $space.isArchived() was exactly what I was looking for.

Perfect for me!

Hi Remo, can we have a solution for cloud version of Confluence. 

Thanks,

This widget could not be displayed.

So I made some modifications to this user macro that I think you may find useful Jo.

Please note, you'll need to update urls with your site and provide the image being called on, browse.png. This is something I used for users to click on to access space permissions (as in the Space Inventory List currently available).

Let me know if you have additional questions

## @param Header:title=Table Header|type=string|required=true|desc=The title for table results.

## @param Label:title=Space Category|type=string|required=true|desc=The category you want to list spaces from.
 
## try to get space category
#set ( $spaceLabel = $action.labelManager.getLabel( "team:${paramLabel}" ) )
 
## check if space category exists
#if ( $!spaceLabel )
    ## try to get spaces by category
    #set ( $spaces = $action.labelManager.getSpacesWithLabel( $spaceLabel ) )
&lt;STYLE TYPE="text/css"&gt;
&lt;!--
.spaceTable { background-color:#F2F2F2;border-collapse:collapse;color:#000;font-size:14px; }
.spaceTable th { background-color:#FFFFFF;color:white;width:auto; }
.spaceTable td, .spaceTable th { padding:5px;border:0; }
.spaceTable td { border-bottom:1px solid #A4A4A4; }
--&gt;
&lt;/STYLE&gt;
    &lt;TABLE BORDER CLASS="spaceTable"&gt;
      &lt;TR&gt;
      &lt;TH border="5" cellpadding="15" width="300px" STYLE="background-color: "&gt;&lt;P ALIGN="center"&gt;
${paramHeader}
     &lt;/P&gt;
     &lt;/TH&gt;
     &lt;/TR&gt;
    #foreach ( $space in $spaces )
        ## check if current user can view space
        #if ( $permissionHelper.canView( $action.remoteUser, $space ) )
     &lt;TR&gt;
            &lt;TD border="5" cellpadding="15" width="auto" STYLE="background-color: #FFFFFF"&gt;
&lt;a href="$!space.urlPath"&gt;$!space.name&lt;/a&gt;&lt;/TD&gt;

&lt;TD border="5" cellpadding="15" width="auto" STYLE="background-color: #FFFFFF"&gt;
&lt;/a&gt;&lt;a href="https://**yoursite**/spaces/spacepermissions.action?key=$!space.key"&gt;&lt;DIV TITLE="View Space Permissions"&gt;&lt;img src="https://**yoursite**/download/attachments/browse.png"&gt;&lt;/DIV&gt;&lt;/a&gt;&lt;/TD&gt;
            &lt;/TR&gt;
        #end
    #end
    &lt;/table&gt;
#else
    &lt;div class="aui-message warning"&gt;
        &lt;p class="title"&gt;
            &lt;span class="aui-icon icon-warning"&gt;Warning&lt;/span&gt;
            &lt;strong&gt;No spaces in this category&lt;/strong&gt;
        &lt;/p&gt;
        &lt;p&gt;Couldn't find space category &lt;strong&gt;$paramLabel&lt;/strong&gt;!&lt;/p&gt;
    &lt;/div&gt;
#end

Interesting … didn't think of the possibility of sorting by using a table!

It doesn't work for me, I'm using Confluence 5.5

Is it possible to force the table to be pre-sorted so I don't need to click on the column header?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday 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...

468 views 6 6
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