I want to move all blogposts from one space to another and set a label for each blogpost
Asked by Stefan Baader:

I created a user macro which works to move all blogposts. But I am not able to add a label. The method createLabel(Label label) needs a value of the type Label, not a string. How to solve that?

```
## Macro title: My Macro## Macro has a body: Y or N## Body processing: Selected body processing option## Output: Selected output option#### Developed by: My Name## Date created: dd/mm/yyyy## Installed by: My Name## This is an example macro## @param OriginSpace:title=Origin Space|type=spacekey|required=true|desc=Space## @param TargetSpace:title=Target Space|type=spacekey|required=true|desc=Space## @param Label:title=Label|type=string|required=false|desc=Label#set($originSpace = $spaceManager.getSpace($paramOriginSpace) )#set($targetSpace = $spaceManager.getSpace($paramTargetSpace) )#set($labelManager = $action.getLabelManager() )#set($blogPosts = $pageManager.getBlogPosts($originSpace, true) )#if($req.getParameter("move") )   #foreach($blogPost in $blogPosts)     &lt;li&gt;move $blogPost to $targetSpace       #if($paramLabel != "")           ##set($label = $labelManager.createLabel($paramLabel) ) #* doesn't work, object Label is required: Label createLabel(Label label) *#           ##$labelManager.addLabel($blogPost, $label)        #end       $blogPost.setSpace($targetSpace)       #if($blogPost.isInSpace($targetSpace)  )          &lt;span style="color:darkgreen"&gt; moved &amp;#9745;&lt;/span&gt;       #end     &lt;/li&gt;   #end#else&lt;h3&gt;Move all blogposts from $originSpace to $targetSpace?&lt;/h3&gt;&lt;form  method="get"&gt;    &lt;input type="submit" name="move" value="move all blogposts listed below"&gt;&lt;/form&gt;&lt;hr/&gt;   #foreach($blogPost in $blogPosts)     &lt;li&gt;$blogPost (Labels:  $blogPost.getLabels() )&lt;/li&gt;   #end#end
```


Answers:
author: Stephen Deutsch
body: There must be some way to do it on the back end, but it's just as easy to do it on the front end. Here's how I would do it:

```
## Macro title: My Macro## Macro has a body: Y or N## Body processing: Selected body processing option## Output: Selected output option#### Developed by: My Name## Date created: dd/mm/yyyy## Installed by: My Name## This is an example macro## @param OriginSpace:title=Origin Space|type=spacekey|required=true|desc=Space## @param TargetSpace:title=Target Space|type=spacekey|required=true|desc=Space## @param Label:title=Label|type=string|required=false|desc=Label#set($originSpace = $spaceManager.getSpace($paramOriginSpace) )#set($targetSpace = $spaceManager.getSpace($paramTargetSpace) )#set($labelManager = $action.getLabelManager() )#set($blogPosts = $pageManager.getBlogPosts($originSpace, true) )#if($req.getParameter("move") )   #foreach($blogPost in $blogPosts)     &lt;li&gt;move $blogPost to $targetSpace       #if($paramLabel != "")           &lt;script&gt;               jQuery.post(contextPath+'/json/addlabelactivity.action', {'entityIdString': '$blogPost.id', 'labelString': '$paramLabel', 'atl_token': jQuery('#atlassian-token').attr('content') });           &lt;/script&gt;       #end       $blogPost.setSpace($targetSpace)       #if($blogPost.isInSpace($targetSpace)  )          &lt;span style="color:darkgreen"&gt; moved &amp;#9745;&lt;/span&gt;       #end     &lt;/li&gt;   #end#else&lt;h3&gt;Move all blogposts from $originSpace to $targetSpace?&lt;/h3&gt;&lt;form  method="get"&gt;    &lt;input type="submit" name="move" value="move all blogposts listed below"&gt;&lt;/form&gt;&lt;hr/&gt;   #foreach($blogPost in $blogPosts)     &lt;li&gt;$blogPost (Labels:  $blogPost.getLabels() )&lt;/li&gt;   #end#end
```

comments: author: Stefan Baader
body: Stephen, many thanks, you made my day ;-)


author: Sameer V
body: This is awesome! thanks Stephen \& Stefan! :)


author: Sara Phillips
body: This does not work for me when I use in the Confluence User Macros, I get same as *nicolarault* below. Outputs code and does not move all the blogs. Is there something that I am missing when creating the User Macro?


author: nicolasrault
body: Hi, I'm a beginner in Macro

I tried this one and all I get in output is HTML code escaped.

Example :

![200812.png](https://community.atlassian.com/t5/image/serverpage/image-id/106455i7760CE65C319DDB2/image-size/large?v=v2&px=999 "200812.png")

What did I forgot ?

I tried all options in "Macro Body Processing"

Thank you  
Nicolas  
France


Tags: user-macro

velocity