Set content-by-label macro with workflow metadata
Asked by Marya Belanger:

The content-by-label macro only automatically applies to labels that were added on page creation, but I would like it to update as labels are added any time after page creation as well.

So I made a workflow parameter in my comala workflow that keeps track of labels.

Is there a way to input whatever the get-metadata macro returns into the content-by-label's cql parameter? I'm looking at the source editor for the template where the content-by-label macro is included, specifically these parameters of the macro:

```
<ac:parameter ac:name="cql">label in ("label1","label2","label3") and type = "page" and space = "space1"</ac:parameter>
<ac:parameter ac:name="labels">label1 label2 label3</ac:parameter>
```

Elsewhere is the get-metadata macro:

```
<ac:structured-macro ac:macro-id="28eb522b-a95e-446f-a595-9ceb4fa4fba6" ac:name="get-metadata" ac:schema-version="1">
    <ac:parameter ac:name="name">Labels</ac:parameter>
</ac:structured-macro>?
```

Without using any third-party plugins, surely there must be a way to pass the output of get-metadata to content-by-label? Possibly by setting a [template variable](https://confluence.atlassian.com/doc/confluence-storage-format-790796544.html#ConfluenceStorageFormat-Templatevariables) equal to the former's output, and then placing that in the latter's parameter declaration?

Something like:

```
<at:declarations>
    <at:list at:name="Labels">
        <ac:structured-macro ac:macro-id="28eb522b-a95e-446f-a595-9ceb4fa4fba6" ac:name="get-metadata" ac:schema-version="1">
            <ac:parameter ac:name="name">Labels</ac:parameter>
        </ac:structured-macro>?
    </at:list>
</at:declarations>

.....

<ac:parameter ac:name="cql">label in $Labels and type = "page" and space = "space1"</ac:parameter>
<ac:parameter ac:name="labels">$Labels</ac:parameter>
```

This doesn't work but if anyone has any idea whether this is on the right track or what can be done, I'd appreciate it! If there's a way to do this directly in the workflow markup itself, that would be even better!


Answers:
author: Nic Brough -Adaptavist-
body: Ok, you shouldn't need to do anything like this.

You say "The content-by-label macro only automatically applies to labels that were added on page creation", but that's not right.

Adding labels to pages that a content-by-label macro is interested in, adds those pages to the results the next time the page the macro is on is rendered (refreshed). I've just tested this with a really simple, mostly empty confluence.

If this is not happening for you, I think I'd want to investigate why it's not working the way it should!

My instinct is that it is something to do with indexing, as that's what the content-by-label macro works off - if the index is not being done properly for pages on edit, that could explain it. There's also a slight quirk with indexing in that it is not immediate, but queued, and if the queue is large, or even stuck, you might not get it updated as quickly as you'd expect.

A quick test would be to check that a new label is not appearing in the macro, run a full re-index (it won't lock or affect your users much, but do it out-of-hours if you're worried), and then check what the macro is showing again.

comments: author: Marya Belanger
body: Slightly different than what you described (I think). I mean whenever a label is added to a page, if that page has a content-by-label macro, can the macro update its cql query with the new label automatically? That's not been my experience so far. I'm not concerned with the *results*of the content by label search being updated (that works fine), I want the macro's cql query to update when new labels are added to the page that it's already on.

Like if I have page1 which already has some \<existing-labels\>, and it has a content-by-label macro that shows other pages with those \<existing-labels\>. And someone adds a new label, label1, after page1 is published. Can page1's content-by-label macro programmatically update its cql query with ".... and label = \<existing-labels\>, page1".

I know that I can use the comala workflow Get Metadata macro in confluence to get the value of a page's @labels@, but can I put that value into a string \<variable\> and set content-by-label to accept "label = \<variable\>"? (all within the source editor?)


author: Daniel Dantas
body: Hi [@Marya Belanger](/t5/user/viewprofilepage/user-id/4529218)

Could you achieve this?   
I have the same requirement here, and it seems it is not possible for me.

Thank you

Daniel


author: Marya Belanger
body: Hi [@Daniel Dantas](/t5/user/viewprofilepage/user-id/4717270) , yes I was able to using the Comala Workflow builder. Looks something like this:

```
{pagefooter:visibility=all}
{get-metadata:Page Footer}
{pagefooter}

{workflowparameter:Page Labels|edit=true}
{workflowparameter}
```

I render the list in the page footer, which we use to designate our sidebar template content on each page. I can't remember if it's relevant for the functionality of this solution (it might be because Page Footer is the only content location on the page that can be dynamically updated through set/get metadata?), but I included it just incase. Then create the Page Labels parameter.

```
.......

{trigger:labeladded|@Page Labels@=@empty@}
{set-metadata:Page Labels}
"@label@"
{set-metadata}
{trigger}

{trigger:labeladded|@Page Labels@=!@empty@}
{set-metadata:Page Labels}
@Page Labels@, "@label@"
{set-metadata}
{trigger}

{trigger:labeladded}
{set-metadata:Page Footer}
 h2. Related articles
 {ContentByLabel:cql=label in (@Page Labels@) and space = "My Space" and title != "@title@"|showLabels=False|showSpace=False}
{set-metadata}
{trigger}
```

The reason I couldn't just use the existing page param for labels is that it outputs them like this: label1,label2,label3. I can't remember if the lack of spaces between the commas was a problem for the cql query or not, but it also didn't allow me to wrap each label in quotations\* which was a bigger problem for me, hence the custom parameter. You can try your luck with just the default page parameter, I think it's called *labels.*

\*I had to add the labels in quotations to the Page Labels param, because the cql query won't parse special characters like hyphens and a lot of our labels have hyphens in them already.

The first trigger adds the label to the parameter if the parameter is empty for that page. You have to add the first one in a separate trigger, because if you only use the second trigger that adds "@label@" to @Page Labels@, @Page Labels@ will be added as an empty value which will cause errors in the workflow.

The *ContentByLabel:cql=...* is really the answer you're looking for. The *title!="@title@"* part keeps the page the macro is rendered on out of the results list, since obviously it will have all the same labels and be the best result.

Let me know if you need more info. My workflow is large and complex so there might be some nuances/dependencies/influences that I left out here for brevity that might cause it not to work.

*** ** * ** ***

As an aside, this would probably make more sense to implement with custom user macro development, but I don't have that level of access at my job and I'm sure there's others like me, so this is a solution for that. If you can create your own user macros, probably just do that instead.


author: Daniel Dantas
body: Hi [@Marya Belanger](/t5/user/viewprofilepage/user-id/4529218) ,

It looks good, but I have a problem.

At my workplace, we don't have Comala, and I just have access to Confluence as a user.

I was expecting to be able to achieve this using the standard plugins/macros.

But anyway, thank you very much for the explanation.


author: James Conway
body: [@Marya Belanger](/t5/user/viewprofilepage/user-id/4529218)

Based on my understanding of your requirements, I don't think that using Comala Document Management to store a workflow metadata item of the labels on a page is necessary.

You can get this data directly from Confluence via the APIs, eg. <https://docs.atlassian.com/ConfluenceServer/rest/7.11.6/#api/content/{id}/label-labels>

I suggest that you explore creating a custom user macro or a custom macro with an app like ScriptRunner. Alternatively you could contact a local Atlassian Solution Partner who can develop such a macro for you.

All the best

James  
Senior Product Manager


Tags: comala-workflows

confluence-server

content-by-label

cql

html

macro

server

source-editor