Thursday, 31 January 2013

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;
   }

No comments:

Post a Comment