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
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);
}
}
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;
}
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;
}
Important and frequently useful blog, thanks Koteshwara
ReplyDelete-Vijay
Can you please share complete code of this. BR//Venki
ReplyDelete