Today we will discuss how to use Custom Attribute in Jsp.Before reading this, you must read my previous blog on Custom Field in Liferay and Jsp Hook in Liferay. Here we create a Jsp hook for create account page , so when user fill the form for create account he also provide maritial status and on click of submit button maritial status is also save with the User . So Lets start this step by step:- Step 1:-Create Custom Field using Control Panel Create Custom field by control panel.For creating this you may follow Custom Field in Liferay.
Step 2:-Change Permission Go to Control panel-->Configuration-->Custom field--> User-->married-->action-->permission. Change permission of guest to view and update also as:-
Step 3:-Create Jsp hook Now create jsp hook that override create_account.jsp .You can see step by step how to create jsp hook in my blog Jsp Hook in Liferay. Now enter this line in create_account jsp as:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Explanation:- Here we are using <liferay-ui:custom-attribute> tag by using this tag we don't need any code for persisting the custom attribute, it automatically saved with User . Otherwise we have to fetch this add by using expando service .
Step 4:-Deploy the hook Now deploy the hook and see the result as:-
when you click submit the user is save in user_ and custom attribute is save in expandorow and expandovalue.
Today we will discuss about Custom Field in Liferay.Lets consider a scenario in which you want that User entity must have one extra field like married. How will you add a new attribute to existing Liferay entity User.Custom fields in liferay is the solution for this problem. Custom fields are designed to allow end users to extends the default data entities like User, Group etc.You can create custom field programmatically as well as Using Control Panel. Today we create Custom Field in User entity by Control Panel. Expando provides mechanism to create tables, columns, rows and values dynamically.Four tables contain entries for custom fields:- a)expandotable
b)expandorow
c)expandocolumn
d)expandovalue
So lets start step by step:- Step 1:-Create Custom Field Go to Control panel-->Configuration-->Custom Fields. A pop up window is open as:-
Click on edit. A new window is open now click on Add Custom Field button as:-
Then a new window is open as:-
Provide key as married and type as true/false and press save. Then press Action-->Edit .here you can edit the property of your custom field as:-
Note:-
The key of custom field is used to access the attribute programmatically through the <liferay-ui:custom-attribute> and <liferay-ui:custom-attribute-list />.
The key must not contain spaces or spacial character.
Step 2:-Check tables entries in database Login in your data base and check entries that are created when you create custom fields .Two tables that are changed are:- a)expandotable
b)expandocolumn
Note:- Only two tables are changed but expandorow and expandovalue are remain same.
Step 3:-Assign custom attribute to User Go to Control panel-->Users and Organisation-->Add-->User
provide all details and save. Then Go to Miscellaneous-->Custom Field-->Select True/false click save.
Step 4:-Check tables entries in database Login in your data base and check entries that are created when you create create User and add Custom fields to that User .Two tables that are changed are:- a)expandorow
b)expandovalue
Note:- ClassPk in expandorow and expandovalue is the UserId of User_ table.
Step 5:-Get the Custom Field in Program Create a portlet and paste this code inside doView() as:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Now login as the User created in Step 3 and deploy the portlet and see the output. Second Approach for getting the value Login as Admin and change the permission of your custom fields as:-
Create a portlet and paste this code inside doView() as:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Today we will discuss about Index Post Processor Hook in Liferay. In Liferay if you want to providing indexing on some column for searching ,sorting etc. than you can use Index Post Processor Hook. Ex-Sign in-->Admin -->Control panel-->Users and organization--> all users. Than try to search user by First Name, Last Name,Screen Name you can search easily but than try to search user by Job Title than no result is found.So today we apply indexing on Job Title by using Index Post Processor Hook.
So lets start step by step:- Step 1:-Create Liferay hook project File-->New-->Liferay Plugin Project-->Provide name-->Select hook in plugin type-->Finish. You can see snapshot in step 2 of Portal Properties Hook in Liferay
Step 2:-Create Class that provide indexing Now create a package com.demo inside src and then create your class that extends BaseIndexerPostProcessor and override postProcessDocument method. CustomIndexer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Explanation:- document.addText(Field.TITLE,jobTitle); Liferay already apply query on the basis of jobTitle.By using addText we send our jobTitle for indexing, so liferay use this in query.For more detail you may refer UserIndexer.java insource code.As a result for example a user has name "Developer" and other user with Job Title "Developer" than in output both result come. Step 3:-Provide entry in liferay-hook.xml Now open liferay-hook.xml click on overview and select indexer class Name as User and IndexerPostProcessor as your class.
This will provide entry in liferay-hook.xml as:- liferay-hook.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 4:-Check output Now deploy the hook and create new user and give some job title. Now try to search this user by Job title.You can easily search this user. Note:- Only those user can be searched by Job title that are created after deployment of Hook . Previous users are not search by Job title as there is no indexing on job title.