Displaying the Space Category

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

Thanks!

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 vote
Davin Studer Community Champion Feb 27, 2014

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

AJS.Meta.get('space-name');

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,897 views 27 12
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