Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
User create from backend java code
Dear All,I created user from backend by using UserLocalServiceUtil as per following code. I tried with both user.setPasswordEncrypted(true) and user.setPasswordEncrypted(false). However in database the password record is saved without encrypted. And also created user can't login. Please suggest. If possible please share some reference link or sample code for liferay 7 user creation with role and permission. Thank youlong userId = CounterLocalServiceUtil.increment(User.class.getName());
_log.info("CounterLocalServiceUtil Done !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
User user = UserLocalServiceUtil.createUser(userId);
user.setCompanyId(companyId);
user.setCreateDate(new Date());
user.setEmailAddress(emailAddress);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
user.setPassword(password);
user.setPasswordEncrypted(true);
user.setReminderQueryAnswer("test");
user.setReminderQueryQuestion("Test?");
user.setScreenName(screenName);
user.setUuid(serviceContext.getUuid());
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(themeDisplay.getCompanyId());
contact.setCreateDate(new Date());
contact.setUserName(screenName);
contact.setUserId(user.getUserId());
contact.setModifiedDate(new Date());
contact.setFirstName("contact-"+contact.getContactId());
contact.setLastName("contact-"+contact.getContactId());
contact.setMiddleName("contact-"+contact.getContactId());
contact.setPrefixId(0);
contact.setSuffixId(0);
contact.setJobTitle(StringPool.BLANK);
contact.setBirthday(new Date());
contact=ContactLocalServiceUtil.addContact(contact);
user.setContactId(contact.getContactId());
UserLocalServiceUtil.addUser(user);
_log.info("CounterLocalServiceUtil Done !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
User user = UserLocalServiceUtil.createUser(userId);
user.setCompanyId(companyId);
user.setCreateDate(new Date());
user.setEmailAddress(emailAddress);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setModifiedDate(new Date());
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
user.setPassword(password);
user.setPasswordEncrypted(true);
user.setReminderQueryAnswer("test");
user.setReminderQueryQuestion("Test?");
user.setScreenName(screenName);
user.setUuid(serviceContext.getUuid());
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
Contact contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(themeDisplay.getCompanyId());
contact.setCreateDate(new Date());
contact.setUserName(screenName);
contact.setUserId(user.getUserId());
contact.setModifiedDate(new Date());
contact.setFirstName("contact-"+contact.getContactId());
contact.setLastName("contact-"+contact.getContactId());
contact.setMiddleName("contact-"+contact.getContactId());
contact.setPrefixId(0);
contact.setSuffixId(0);
contact.setJobTitle(StringPool.BLANK);
contact.setBirthday(new Date());
contact=ContactLocalServiceUtil.addContact(contact);
user.setContactId(contact.getContactId());
UserLocalServiceUtil.addUser(user);
Attachments:
You should not be doing any of this nonsense.
Instead, you should just be calling userLocalService.addUserWithWorkflow(), populating all of the parameters.
And we don't use the static *LocalServiceUtil classes any more. Those are just for legacy code only. If you are using them at all, it is an indication that you are doing something wrong.
Instead, you should just be calling userLocalService.addUserWithWorkflow(), populating all of the parameters.
And we don't use the static *LocalServiceUtil classes any more. Those are just for legacy code only. If you are using them at all, it is an indication that you are doing something wrong.
Hi Daviad,
Thank you for your response. Is there any reference code or sample to download for liferay 7 version user creation from backend? I am taking sample from
https://liferay.dev/forums/-/message_boards/message/14332034 , http://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.html, http://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.html. But all are very old.
Thank you for your response. Is there any reference code or sample to download for liferay 7 version user creation from backend? I am taking sample from
https://liferay.dev/forums/-/message_boards/message/14332034 , http://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.html, http://tanajilondhe.blogspot.com/2013/01/liferay-add-user-programmatically.html. But all are very old.
The last one of your links looks pretty ok.
But the best place for examples is often the Liferay sourcecode itself. e.g. here:
https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/login/login-web/src/main/java/com/liferay/login/web/internal/portlet/action/CreateAccountMVCActionCommand.java
(I searched in the github repository for addUserWithWorkflow and that was one of the hits using that method)
We usually create the user with addUserWithWorkflow and then we set any extra fields (that the method doesn't have) afterwards. The method does all the necessary things like encrypting the password.
But the best place for examples is often the Liferay sourcecode itself. e.g. here:
https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/login/login-web/src/main/java/com/liferay/login/web/internal/portlet/action/CreateAccountMVCActionCommand.java
(I searched in the github repository for addUserWithWorkflow and that was one of the hits using that method)
We usually create the user with addUserWithWorkflow and then we set any extra fields (that the method doesn't have) afterwards. The method does all the necessary things like encrypting the password.
Thank you for reference Christoph.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™