Displaying the Space Category

How would one go about displaying the Space Category within a user macro?


2 answers

Ok, I think I have a potential solution: Since we have a naming standard for putting the space category in the name of the space details name, the meta tags of the rendered pages store a number of various tidbits of useful information, including a meta tag named ajs-space-name. So, I used a simple JavaScript loop to run through all the meta tags until it found that specific tag. From there, I'm able to extract the value from the tag's content attribute.

var metas = document.getElementsByTagName('meta');
	for (var i = 0; i < metas.length; i++) { // loop through all the meta tags
		var metaName = metas[i].name; // grab all the meta tag's name attributes
		if (metaName == "ajs-space-name") { // name of meta tag that holds the space category
			var metaContent = metas[i].content; // get the content value for target
			var spaceCategory = metaContent.substring(0, metaContent.indexOf(":")); // only hold string that ends up to ":"

Does that look fool proof?

Note: This does not grab space categories for personal spaces.

0 votes
Davin Studer Community Champion Feb 27, 2014

A better way of getting the meta data is to use ...


This is more future proof.

Davin Studer Community Champion Feb 27, 2014

What if they decided that they no longer will put that data in the page on load? Rather it will be called asynchronously via the Meta.get() method if the client requests it. Or what if they change the name of the meta tag? In that case your code would fail but if you use the API they have built for this purpose it would continue to work. That is why I say it is a little more future proof. Since they built and API for getting the data I would use that. Plus it is fewer lines of code ... one vs eight.

How often does the meta tags values and content change? Forgive my ignorance but how is this more future proof?

I cannot argue with the likelihood of Atlassian changing what data goes into the meta tags but the method you mentioned relies on the Confluence Admins (me) to provide the category of the space in the name of space (editing Space Details > Name > <type>: Foo) rather than relying on the information provided in Space Categories in the same Space Details page. I would have to use some JS to extract the string up to the ":" and use that string for the data I'm looking for.

A sample output from running a test with AJS.Meta.get("space-name"):

Personal space: firstname lastname

department space: Dept: deptname

I would also have to take that into account that individual users won't be adding the prefix of "Personal" to their respective space names nor adding a Space Category.

Davin Studer Community Champion Mar 17, 2014

If you want to use the menthod you mentioned above that would indeed be the case. I was simply saying that using the AJS.Meta.get methods is better than looping though all the meta tags like you were doing before.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Feb 06, 2019 in Confluence

Try out the new editing experience

Hi team, I’m Avinoam, a product manager on Confluence Cloud, and today I’m really excited to let the Community know that all customers can now try out the new editing experience and see some of the ...

783 views 33 5
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