Thursday, 17 September 2015

Create User By Programatically


Liferay - add user programmatically

Create Liferay Portal User Programmatically

final ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest
               .getAttribute(WebKeys.THEME_DISPLAY);
User user = createPortalUser("tanaji", themeDisplay.getCompanyId(),
               themeDisplay.getUserId(), themeDisplay.getLocale());
System.out.println("Created User : "+user);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create new Portal User function.
private User createPortalUser(String userName, final long companyId,
           final long userId, final Locale locale) {

       User user = null;
       try {
           user = UserLocalServiceUtil.addUser(userId, companyId, false,
                   "test", "test", false, userName + "screenName", userName
                           + "@liferay.com", 0L, "", locale, userName
                           + "firstName", "middleName", userName + "lastName",
                   0, 0, false, 0, 1, 1970, "Job Title", null, null, null,
                   null, false, new ServiceContext());

       } catch (Exception e) {
           System.out.println("Portal user creation failed " + e.getMessage());
       }
       return user;
   }
Add and Update an  User programmatically in liferay 6.1.20

If you want to add user in liferay. You just follow the below steps


  •  provide all mandatory fields like first name, screen name, email address

          long[] userGroupIds = {};
long[] roleIds = { ParamUtil.getLong(actionRequest, "roleId") };
long[] organizationIds = new long[] { ParamUtil.getLong(actionRequest,
"organizationId") };
long[] groupIds = {};
     
          ServiceContext serviceContext = ServiceContextFactory.getInstance(
User.class.getName(), actionRequest);

 public static User addUserDetails(long companyId, long roleId,
String password1, String password2, String screenName,
String emailAddress, String firstName, String middleName,
String lastName, long[] groupIds, long[] organizationIds,
long[] roleIds, long[] userGroupIds, String street1,
String street2, String street3, String city, String state,
String zip, long countryId, String officeNumber,
String mobileNumber, String homeNumber, String faxNumber,
String linkedInId, String facebookSn, String twitterSn,
long annualIncome, String currencyCode, String domainExpertise,
String experience, String qualification, boolean isSubScribe,
String aboutIway, String className, ServiceContext serviceContext)
throws PortalException, SystemException {

User user = null;
boolean flag = false;

user = UserLocalServiceUtil.addUser(0, companyId, false, password1,
password2, false, screenName, emailAddress, 0,
StringPool.BLANK, LocaleUtil.getDefault(), firstName,
middleName, lastName, 0, 0, true, Calendar.JANUARY, 1, 1979,
StringPool.BLANK, groupIds, organizationIds, roleIds,
userGroupIds, false, serviceContext);
if (Validator.isNotNull(user)) {
user = updateUserDetails(user.getUserId(), user.getPassword(),
StringPool.BLANK, StringPool.BLANK, screenName,
emailAddress, firstName, middleName, lastName, facebookSn,
twitterSn, groupIds, organizationIds, roleIds,
Collections.EMPTY_LIST, userGroupIds, true, serviceContext);
}
if (Validator.isNotNull(user)) {
flag = addPhone(user.getUserId(), user.getContactId(),
officeNumber, mobileNumber, homeNumber, faxNumber,
className);
}
if (flag) {
flag = addAddress(user.getUserId(), user.getContactId(), street1,
street2, street3, city, zip, countryId, className);
}

}


  • Once add the user and if you want to add user info in contact table then you need to follow the below snippet.

   public static User updateUserDetails(long updateUserId, String oldPassword,
String newPassword1, String newPassword2, String screenName,
String emailAddress, String firstName, String middleName,
String lastName, String facebookSn, String twitterSn,
long[] groupIds, long[] organizationIds, long[] roleIds,
List<UserGroupRole> userGroupRoles, long[] userGroupIds,
boolean isUserAdd, ServiceContext serviceContext) {
User user = null;
try {

user = UserLocalServiceUtil.updateUser(updateUserId, oldPassword,
newPassword1, newPassword2, false, StringPool.BLANK,
StringPool.BLANK, screenName, emailAddress, 0,
StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
StringPool.BLANK, StringPool.BLANK, firstName, middleName,
lastName, 0, 0, true, Calendar.JANUARY, 1, 1979,
StringPool.BLANK, StringPool.BLANK, facebookSn,
StringPool.BLANK, StringPool.BLANK, StringPool.BLANK,
StringPool.BLANK, StringPool.BLANK, twitterSn,
StringPool.BLANK, StringPool.BLANK, groupIds,
organizationIds, roleIds, userGroupRoles, userGroupIds,
serviceContext);
} catch (PortalException e) {
e.printStackTrace();
if (isUserAdd) {

deleteUser(updateUserId);
}
} catch (SystemException e) {
e.printStackTrace();
if (isUserAdd) {

deleteUser(updateUserId);
}
}
return user;

No comments:

Post a Comment