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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

User Macro Add parameter value to AddLabel

Hello!

I have a requirement to add the value of a parameter to a page as a label. 

The user macro is a subset of fields we are asking our users to provide.  In this example We are asking for a Document Number. The user provides this number and I wanted to add this as a label to the page.  The macro below includes a script that adds a Box with a plus icon. The user would enter the label and click on add.  I thought that I could reuse some of this macro to add a label but haven't been able to achieve this.   Ideally the user would not be able to see the input box or the add icon. The code would take the $paramDocNum and add it to the label.

Any suggestions would be greatly appreciated.  

Melissa

## @param DocNum:title=Document Number |type=string|required=false|desc=Enter your Document ID #


<script>
var addLabel = function(label) {
jQuery.post(contextPath + "/json/addlabelactivity.action", {"entityIdString": "$content.id", "labelString": label, "atl_token": jQuery("#atlassian-token").attr("content") }, function() {
jQuery("#label-to-add").val("");
});
}
</script>
<form class="aui" onsubmit="return false;">
<input id="label-to-add" class="text">
<button class="aui-button aui-button-subtle" onclick="addLabel(jQuery('#label-to-add').val());">
<span class="aui-icon aui-icon-small aui-iconfont-add">Add </span>
</button>
</form>

<br>
<tr>
<td>Document ID #<br></td>
<td> $paramDocNum
</td>
</tr>

 

2 answers

1 accepted

2 votes
Answer accepted

Hi Melissa,

This should work:

## @param DocNum:title=Document Number|type=string|required=false|desc=Enter your Document ID #

#if ( $paramDocNum )
    #set ( $hasLabel = false )
    #foreach ( $label in $content.getLabels() )
        #if ( $label == $paramDocNum )
            #set ( $hasLabel = true )
        #end
    #end
    #if ( !$hasLabel )
        <script>
            AJS.toInit(function() {
                jQuery.post(contextPath + "/json/addlabelactivity.action", {
                    "entityIdString": "$content.id",
                    "labelString": "$paramDocNum",
                    "atl_token": jQuery("#atlassian-token").attr("content")
                });
            });
        </script>
    #end
    <table>
        <thead>
            <tr>
                <th>Document ID #</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td> $paramDocNum</td>
            </tr>
        </tbody>
    </table>
#end

@Stephen Deutsch

Thank you!!!! I can't thank you enough! I would of never figured this out!

If I wanted to add the label to the child pages could I add this ?

#set($childpages = $content.getChildren())
#foreach($page in $childpages)

Hi Melissa,

It would work to insert those two lines after the first line, add an #end at the end of the document, and change $content.id to $page.id, but this would only add labels to the children and not to the page itself. Therefore I would recommend to copy the body code (i.e. excluding the first line), make the changes that I mentioned (also remove the <table> tags and everything in between so that the DocNum doesn't display multiple times), and paste this underneath the existing code.

You should be aware of 2 more things:

1 - getChildren gets only the direct children of the page in question, any children of the children are not included:

-Page 

  -Child (included)

     -Child of Child (not included)

If you want to also include the child of the child, this is referred to in the internals of Confluence as a "descendant", so instead of getChildren, you would call getDescendants.

2 - This checks every label of a page, so if you include checking all children/descendants and there are a lot of pages, it could slow down loading (probably not, but it might).

Thanks Again @Stephen Deutsch

I made your changes and it's working !

Hoi @Stephen Deutsch 

I'm trying to build a user macro that allows the user to add specific labels to a page using checkboxes. I will use this macro for the template

When user choose a macro, they can enter the label name and the label value

When user create a new document from template, they can see a checkbox

After checked on the checkbox and save, the label will be added to page

I tried from your macro but I can't make it work, so please give me a suggestion.

Thank you

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

🏑 Atlympic Event: Confluence

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

170 views 6 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you