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

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 !!!

 

1 answer

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.  

Suggest an answer

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

🏠 Say hello to the new Confluence Home!

Hi Atlassian Community, My name is DJ Chung, and I’m a Product Manager on the Confluence Cloud team. Today, I’m excited to share a new and improved version of Home. The new Home helps you ...

1,003 views 7 42
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