Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Daniel Aschauer
Create custom attributes programmatically
4. April 2013 05:27
Antwort

Daniel Aschauer

Rang: Junior Member

Nachrichten: 39

Eintrittsdatum: 22. Oktober 2012

Neue Beiträge

I have a portlet that needs to use some extra custom attributes in the user class.
Thus, to use the portlet first one has to create the custom field in the User first.
Is there way to define that field in the plugin itself? Or to call a method by the installation of the plugin, that then creates the expando definition?
Thanks,
Daniel
Apoorva Prakash
RE: Create custom attributes programmatically
6. April 2013 05:28
Antwort

Apoorva Prakash

Rang: Liferay Master

Nachrichten: 659

Eintrittsdatum: 15. Juni 2010

Neue Beiträge

Daniel Aschauer:
I have a portlet that needs to use some extra custom attributes in the user class.
Thus, to use the portlet first one has to create the custom field in the User first.
Is there way to define that field in the plugin itself? Or to call a method by the installation of the plugin, that then creates the expando definition?
Thanks,
Daniel


Hello Daniel,

Hope the following code block may help you.

1user.getExpandoBridge().addAttribute("attributeName");


1user.getExpandoBridge().setAttribute("attributeName", "value");


1if (user.getExpandoBridge().hasAttribute("attributeName")) {
2... write your custom code here
3 };


See this link as will.

HTH.

Thanks and Regards,
Apoorva Prakash
Daniel Aschauer
RE: Create custom attributes programmatically
8. April 2013 02:55
Antwort

Daniel Aschauer

Rang: Junior Member

Nachrichten: 39

Eintrittsdatum: 22. Oktober 2012

Neue Beiträge

Hi Apoorva!

Thanks for you advice! In the meantime I solved the issue in some other way. As I mentioned my goal was to add (an) new definition(s) for custom attributes.

I didn't try out your code sample user.getExpandoBridge().addAttribute("attributeName"); Where/How can you define the type of the attribute you are adding this way? Note, that you as well already have to use a concrete user object.

I am using the following method now to add new definitions for custom attributes:
 1    public static ExpandoColumn addExpandoAttribute(long companyId, String columname, int type, String className) throws PortalException, SystemException {
 2
 3        ExpandoTable table = null;
 4        ExpandoColumn exandoColumn = null;
 5       
 6        try {
 7                /*A little note about this:
 8                  Using ExpandoTableConstants.DEFAULT_TABLE_NAME you are trying to add the default expando column.
 9                              And it will always fail, because that table aready exists within liferay.
10                              You can change this value to create a new table, for example:
11                  table = ExpandoTableLocalServiceUtil.addTable(companyId, User.class.getName(), "MYTABLE");
12                  And of course you can change User.class.getName(), to add expando values to any other liferay model.
13                  Usign ExpandoTableConstants.DEFAULT_TABLE_NAME You are going to be able to see and modify them as Custom fields from control Panel.
14                  If you create other table, you are not going to be able to modify them from control panel.
15                */
16            table = ExpandoTableLocalServiceUtil.addTable(
17                companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
18             }
19             catch (DuplicateTableNameException dtne) {
20                 /* Get the default table for User Custom Fields */
21                 table = ExpandoTableLocalServiceUtil.getTable(companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
22            }
23         
24          try {   
25              exandoColumn = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),columname, type);
26          }
27         catch (DuplicateColumnNameException dcne)
28         {
29             exandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, className,ExpandoTableConstants.DEFAULT_TABLE_NAME,columname);
30         }
31         return exandoColumn;
32    }


I do call this method for the attributes I want to create in the portlets init()-method. Intentionally I wanted to add the code in some place that is only executed when the plugin is installed/deployed, but I am afraid there is no such possibility in liferay, or is there?