Java
Java

Reputation: 2489

Assign site template to organization site programmatically in liferay 6.1.0

I have created user & organization pragmatically using addUser() & addOrganization() methods respectively.

I am also able to add users to this organization using addOrganizationUsers() method.

Now I have created a site template from liferay control panel.

As we know , we can create a site for organization, and while creating a site we have options to select a site template for public & private pages.

As we know . Public page - Visible to members + non members Private page - Visible to only members.

So I want to create a organization site with private pages only so it will be seen by only organization member.

OrganiztionLocalServiceUtil.addOrganization(
    long userId, long parentOrganizationId, String name, String type,
    boolean recursable, long regionId, long countryId, int statusId,
    String comments, boolean site, ServiceContext serviceContext)

Using above method , by specifying boolean site value 'true' a site will get created.

Now I want to add a site template to this organization site pragmatically which I have created from control panel.

So is there any API to add site template to any site of organization

Upvotes: 3

Views: 1573

Answers (1)

schup
schup

Reputation: 2104

Unfortunately there is no public API for it.

Use LayoutSetPrototypeLocalServiceUtil to get the ID for the SiteTemplate. To get the SiteTemplate by name you'll have to either use a dynamicQuery or iterate over the result of LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypes(-1, -1)

Then invoke applyLayoutSetPrototypes of SitesUtil in the context of the portal.

MethodKey methodKey = new MethodKey("com.liferay.portlet.sites.util.SitesUtil","applyLayoutSetPrototypes", Group.class, long.class, long.class, ServiceContext.class);
PortalClassInvoker.invoke(false, methodKey, organization.getGroup(), publicLayoutSetId, privateLayoutSetId, serviceContext);

Specify -1 for publicLayoutSetId.

An Admin has to be logged in to perform this action. To perform this action on startup or in the background a new ServiceContext would be needed.

Something like the following

ServiceContext serviceContext = new ServiceContext();
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setSignedIn(false);
// set the following to an admin user / company or default user
User user = UserLocalServiceUtil.getDefaultUser(companyId); // or any user that has the permissions
serviceContext.setUserId(user.getUserId());
serviceContext.setCompanyId(companyId);

And most likely you also have to setup the ThreadPermissionChecker

PrincipalThreadLocal.setName(user.getUserId());
PermissionChecker adminPermissionChecker = PermissionCheckerFactoryUtil.create(user, false);
PermissionThreadLocal.setPermissionChecker(adminPermissionChecker);

Don't forget to reset the permission checker in a final block otherwise the same permission checker might be used for other requests on the same thread.

Upvotes: 2

Related Questions