Foren

Create custom attributes programmatically

Daniel Aschauer, geändert vor 11 Jahren.

Create custom attributes programmatically

Junior Member Beiträge: 39 Beitrittsdatum: 22.10.12 Neueste 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
thumbnail
Apoorva Prakash, geändert vor 11 Jahren.

RE: Create custom attributes programmatically

Liferay Master Beiträge: 658 Beitrittsdatum: 15.06.10 Neueste 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.

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


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


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


See this link as will.

HTH.

Thanks and Regards,
Apoorva Prakash
Daniel Aschauer, geändert vor 11 Jahren.

RE: Create custom attributes programmatically

Junior Member Beiträge: 39 Beitrittsdatum: 22.10.12 Neueste 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:
	public static ExpandoColumn addExpandoAttribute(long companyId, String columname, int type, String className) throws PortalException, SystemException {

		ExpandoTable table = null;
        ExpandoColumn exandoColumn = null;
		
        try {
	            /*A little note about this:
	              Using ExpandoTableConstants.DEFAULT_TABLE_NAME you are trying to add the default expando column.
	                          And it will always fail, because that table aready exists within liferay.
	                          You can change this value to create a new table, for example:
	              table = ExpandoTableLocalServiceUtil.addTable(companyId, User.class.getName(), "MYTABLE");
	              And of course you can change User.class.getName(), to add expando values to any other liferay model.
	              Usign ExpandoTableConstants.DEFAULT_TABLE_NAME You are going to be able to see and modify them as Custom fields from control Panel.
	              If you create other table, you are not going to be able to modify them from control panel.
	            */
            table = ExpandoTableLocalServiceUtil.addTable(
                companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
         	}
         	catch (DuplicateTableNameException dtne) {
         		/* Get the default table for User Custom Fields */
         		table = ExpandoTableLocalServiceUtil.getTable(companyId, className, ExpandoTableConstants.DEFAULT_TABLE_NAME);
        	}
         
 	     try {   
 	    	 exandoColumn = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(),columname, type);
 	     }
    	 catch (DuplicateColumnNameException dcne) 
    	 { 
    		 exandoColumn = ExpandoColumnLocalServiceUtil.getColumn(companyId, className,ExpandoTableConstants.DEFAULT_TABLE_NAME,columname);
    	 }
 	    return exandoColumn;
	}


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?