How I can update the confluence users email addresses in bulk?
Asked by Suresh Sakhare:

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


Answers:
author: Suresh Sakhare
body:     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.


author: Jessie Wang_ScriptRunner_The Adaptavist Group
body: 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>


Tags: confluence-server

scriptrunner

server