Monday, 21 September 2015

How to Add organization and User by Programatically in liferay

Add Organization by programatically

private Organization CreateRegularOrg(String name, long companyId) {
       
ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
serviceContext.setCompanyId(companyId);
Organization org = null;
try {
org = OrganizationServiceUtil.addOrganization(0, name,"regular-organization", true, 19014, 19, 12017,StringPool.BLANK, true, serviceContext);
org.setCompanyId(companyId);

org = OrganizationLocalServiceUtil.updateOrganization(org);
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}


return org;
}



How to Programmatically Add Liferay Users 

private void addOrgUser(Organization org, String ScreenName,String EmailAddress, ActionRequest actionRequest)
throws PortalException {
Contact contact =null;
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
System.out.println("org.getCompanyId()  " +org.getCompanyId());
String greeting="Welcome Admin";
try {

User user = UserLocalServiceUtil.createUser(CounterLocalServiceUtil.increment());

user.setFirstName(FirstName);

user.setEmailAddress(EmailAddress);

user.setScreenName(ScreenName);
user.setCompanyId(org.getCompanyId());

user.setCreateDate(new Date());
user.setModifiedDate(new Date());
user.setGreeting(greeting);
user.setLanguageId(themeDisplay.getLanguageId());
user.setTimeZoneId(themeDisplay.getTimeZone().getDisplayName());
long idContact = CounterLocalServiceUtil.increment(Contact.class.getName());
contact = ContactLocalServiceUtil.createContact(idContact);
contact.setCompanyId(org.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());
                  // if u want to create user inside organization than u have to use                  addOrganizationUser method
UserLocalServiceUtil.addOrganizationUser(org.getOrganizationId(),user);
UserLocalServiceUtil.addUser(user);


} catch (SystemException e) {
e.printStackTrace();
}



}

/*----Calling CreateRegularOrg method and addOrgUser method ---------*\

Organization org = null;
org = CreateRegularOrg(name, company.getCompanyId());
            

            addOrgUser(org, tenantAdminName,tenantAdminEmailId,actionRequest);


2 comments:

  1. how can I create site programmatically. Please help. Thanks in advance.

    ReplyDelete
  2. Hello I wanted to create site in life ray by json data.How can i achieve this please guide.

    ReplyDelete