Hi guys!
Last week I created the sample expando hook, a simple example to show how easy is to extend portal entities on startup without using the UI.
The main goal of this work method is to allow Liferay users to extend portal entities from a plugin, so that they don't need to repeat the same action (the creation of new fields for their entities) in every installation they do.
You can find it ready to use in the plugins' hooks svn ( http://svn.liferay.com/browse/plugins/trunk/hooks)
I have based this development in this Ray's blog entry ( http://www.liferay.com/web/raymond.auge/blog/-/blogs/adding-expandos-from-a-startup-hook) and here's the explaination so that you can adapt it to your concrete use case:
In portal.properties you declare your class so that it is launched on startup:
application.startup.events=com.liferay.sampleexpando.hook.events.StartupAction
In StartupAction, in doRun, you do the main things:
1.- We get a reference of (or create) the table ExpandoTableConstants.DEFAULT_TABLE_NAME, determining the portal entity we are working with (in this case it'll be the calendar events - CalEvent)
try { expandoTable = ExpandoTableLocalServiceUtil.addTable( CalEvent.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME); } catch (Exception e) { expandoTable = ExpandoTableLocalServiceUtil.getTable( CalEvent.class.getName(), ExpandoTableConstants.DEFAULT_TABLE_NAME); }
2.- We add the new column using a custom name ("sampleColumn") and its type (ExpandoColumnConstants.STRING in this case):
ExpandoColumnLocalServiceUtil.addColumn(
expandoTable.getTableId(), "sampleColumn",
ExpandoColumnConstants.STRING);
And that's all!
I hope it'll be useful
Regards
Juan Fernández