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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,739
Community Members
 
Community Events
176
Community Groups

How I can update the confluence users email addresses in bulk?

Edited

Hi

 

One of our use case, We need to update the all confluence users email addresses. I was trying to implement it through script, but somehow its not working. I was tried with UserAccessor & PersonalInformationManager services and its failing.

com.atlassian.user.User usr = (com.atlassian.user.User)userManager.getUser("suressak");
String email = "sureshsakhare@xxxx.com";
usr.setEmail(email);
userAccessor.saveUser(usr)
return "updated"
com.atlassian.user.User user = (com.atlassian.user.User)userAccessor.getUser("suressak");
if(personalInformationManager.hasPersonalInformation("suressak")){
PersonalInformation personalInfo = personalInformationManager.getOrCreatePersonalInformation(user);
personalInfo.setEmail("sureshsakhare@xxx.com")
personalInformationManager.savePersonalInformation(personalInfo,personalInformationManager.getOrCreatePersonalInformation(usr));
return "updated"
}

 

Can you help on this? How I can achieve it?

Confluence Data center version : 6.6.11.

We are also using scriptrunner plugin. 

Thanks in advanced !!!

 

2 answers

1 accepted

0 votes
Answer accepted
import com.atlassian.sal.api.component.ComponentLocatorimport org.apache.log4j.Levelimport org.apache.log4j.Loggerimport com.atlassian.user.User     import com.atlassian.confluence.user.UserAccessorimport com.atlassian.confluence.user.ConfluenceUserimport com.atlassian.user.impl.DefaultUser  Logger  log = Logger.getLogger("com.xxx.updateusers")log.setLevel(Level.ALL)         try{log.info("<------------------------------ User update activity started  -------------------------------------------->");File file = new File('/confluence-sb-uat/shared-home/scripts/azure_confluence_users.csv')def csvMapList = []  file.eachLine { line ->    def columns = line.split(",")    def tmpMap = [:]      tmpMap.putAt("original_username", columns[0])    tmpMap.putAt("new_username", columns[1])    tmpMap.putAt("original_email", columns[2])    tmpMap.putAt("new_email", columns[3])     csvMapList.add(tmpMap)}final String stringConstant = "New";csvMapList.each { map ->    try{                 String originalUserName = map.getAt("original_username")        String newUserName = map.getAt("new_username")        String originalEmail = map.getAt("original_email")        String newEmail = map.getAt("new_email"        log.info(originalUserName+" : "+newUserName+" : "+originalEmail+" : "+newEmail);                UserAccessor userAccessor =  ComponentLocator.getComponent(UserAccessor)          if(userAccessor.exists(originalUserName)){            if(userAccessor.exists(newUserName)){                                   String renameNewUser = newUserName + stringConstant;                String renameEmail = newEmail + stringConstant;                /*Rename the new user's username & email*/                      ConfluenceUser usr = userAccessor.getUserByName(newUserName);                                  final DefaultUser mNewUser = new DefaultUser((User) usr);                    mNewUser.setEmail(renameEmail);                    userAccessor.saveUser((User)mNewUser);                                      userAccessor.renameUser(usr,renameNewUser);                                   /*disabled new user*/                userAccessor.deactivateUser((com.atlassian.user.User) userAccessor.getUserByName(renameNewUser));                                                   /*Update originalUser with New user's username & Email*/                try{                    ConfluenceUser originalUser = userAccessor.getUserByName(originalUserName);                    final DefaultUser mOriginalUser = new DefaultUser((User) originalUser);                    mOriginalUser.setEmail(newEmail);                    userAccessor.saveUser((User)mOriginalUser);                                          userAccessor.renameUser(originalUser,newUserName);                    log.info(originalUserName+" :Old user updated successfully with new user's username:"+newUserName+" & email:"+newEmail);                }catch(e){                                        log.error("Something went wrong while updating the old user. Rollbacking new user rename operation. User:"+newUserName);                     ConfluenceUser usrRollback = userAccessor.getUserByName(renameNewUser);                                   userAccessor.reactivateUser((com.atlassian.user.User) usrRollback);                                           final DefaultUser mNewUserRollback = new DefaultUser((User) usrRollback);                     mNewUserRollback.setEmail(newEmail);                     userAccessor.saveUser((User)mNewUserRollback);                                       userAccessor.renameUser(usrRollback,newUserName);                                                                            }                                                                                    }else{                log.warn("New user not created yet. We are skiping the user update action for user:"+originalUserName);            }                       }else{            log.warn("user not found:"+originalUserName);        }             }catch(e){        log.warn("Something went wrong while updating the user : "+map.getAt("original_username")+" due to "+e.getMessage());     }       }log.info("<------------------------------ User update activity end  -------------------------------------------->");}catch(e){    log.error("Something went wrong while updating the user "+e);}

I tried it successfully. This may help someone.  

Hi all, this script is now available in our script library for ScriptRunner for Confluence Server/DC (tested by our engineers).

Feel free to copy or customise it as you wish https://library.adaptavist.com/entity/update-user-emails-in-bulk

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events