Message Boards

Create Page Programmatically

Nived Rajan, modified 11 Years ago.

Create Page Programmatically

Junior Member Posts: 36 Join Date: 5/2/12 Recent Posts
how to create liferay public page in Programmatically?
thumbnail
Vitaliy Koshelenko, modified 11 Years ago.

RE: Create Page Programmatically

Expert Posts: 319 Join Date: 3/25/11 Recent Posts
Something like this:

    long userId = themeDisplay.getUserId();
    long groupId = themeDisplay.getScopeGroupId();
    ServiceContext serviceContext = new ServiceContext();
    LayoutLocalServiceUtil.addLayout(
            userId,
            groupId,
            false, //not private
            0,     //has no parent layout
            "some_name",
            "some_title",
            "some_description",
            "portlet",   //type
            false,       //not hidden
            "/new-page", //friendly URL
            serviceContext    
    );


..but I have not tested it yet.
thumbnail
Enrique Valdes Lacasa, modified 8 Years ago.

RE: Create Page Programmatically

Junior Member Posts: 92 Join Date: 7/29/14 Recent Posts
Using Liferay Layouts APIs, here is the code that works for me in 6.2:

ServiceContext serviceContext = new ServiceContext();
Layout myPage =  LayoutLocalServiceUtil.addLayout(userId, groupId, false, 
                                        0, "My Page", "Products", "My sample page", 
                                        LayoutConstants.TYPE_PORTLET, false, "/myPage", serviceContext);


userId: user adding the page
groupId: the id of the site
false: specifies it as a public page
LayoutConstants.TYPE_PORTLET: the type of page (in this case, a portlet page)
/myPage: the friendly URL selected for that page
serviceContext: allows us to specify more attributes for the page

Let's say you want to apply a page template for the page you create. Well, the serviceContext allows you to specify the page template (or layout prototype) in an attribute, according to the LayoutLocalServiceUtil.addLayout() documentation, where the method is well described. That would be done like this:

Note how you will need the page template Uuid for that, which is a lengthy String to identify page templates
LayoutPrototype pageTemplate = LayoutPrototypeLocalServiceUtil.getLayoutPrototype(layoutPrototypeId);
String templateUuid = pageTemplate.getUuid();

ServiceContext serviceContext = new ServiceContext();
serviceContext.setAttribute("layoutPrototypeUuid", templateUuid);
serviceContext.setAttribute("layoutPrototypeLinkedEnabled", true);

Layout myPage = LayoutLocalServiceUtil.addLayout(userId, groupId, false, 
                                         0, "My Page", "Products", "My sample page", 
                                         LayoutConstants.TYPE_PORTLET, false, "/myPage", serviceContext);
Sanat D, modified 3 Years ago.

RE: Create Page Programmatically

New Member Posts: 17 Join Date: 9/8/20 Recent Posts
i can create widget page by using above code. but can anyone tell me how can I create a content page by programmatically? should i have change any parameter type of the page and if yes then which type should I can use to create content page?  Please reply.
Thanks in advance.
thumbnail
Mohammed Yasin, modified 3 Years ago.

RE: Create Page Programmatically

Liferay Master Posts: 591 Join Date: 8/8/14 Recent Posts
Hi,
You can use LayoutConstants.TYPE_CONTENT type for creating content page.
thumbnail
Olaf Kock, modified 3 Years ago.

RE: Create Page Programmatically

Liferay Legend Posts: 6400 Join Date: 9/23/08 Recent Posts
Sanat D:

i can create widget page by using above code. but can anyone tell me how can I create a content page by programmatically? should i have change any parameter type of the page and if yes then which type should I can use to create content page?  Please reply.
On top of Mohammed's hint: This portlet might help with figuring out several settings.