1. Get all the attribute names of any Entity
long companyId = CompanyThreadLocal.getCompanyId(); ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(companyId, User.class.getName()); Enumeration<String> attributes =Â expandoBridge.getAttributeNames(); while(attributes.hasMoreElements()){ System.out.println(attributes.nextElement()); }
The above code will give all the Custom Fields of User.
2. Get all Attribute Names as well as Value
long companyId = CompanyThreadLocal.getCompanyId(); ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(companyId, User.class.getName()); Map<String,Serializable> attributeNameValuePair =Â expandoBridge.getAttributes(); for (Map.Entry<String, Serializable> entry : attributeNameValuePair.entrySet()) { System.out.println("Attribute Name = " + entry.getKey() + ", Attribute Value = " + entry.getValue()); }
The above code will give all the custom attributes of User Entity as well as its corresponding values.
3. Check how many custom fields are there for an Entity
long companyId = CompanyThreadLocal.getCompanyId(); ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(companyId, User.class.getName()); List<String> attributeList = Collections.list(expandoBridge.getAttributeNames()); if(attributeList.size() == 0){ System.out.println("There are no custom fields associated with the Entity"); }else{ System.out.println("There are "+attributeList.size()+" attributes for the Entity"); }
The above code will check whether there are custom fields for User or not
4. Get properties of specific custom field
long companyId = CompanyThreadLocal.getCompanyId(); ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(companyId, User.class.getName()); UnicodeProperties unicodeProperties = expandoBridge.getAttributeProperties("address"); String height = unicodeProperties.getProperty(ExpandoColumnConstants.PROPERTY_HEIGHT); System.out.println("Height property of address :"+height);
The above code can be used to get properties of a specific custom field. In the above case the custom field is address. We can use ExpandoColumnConstants to access all the key and pass the key in unicodeProperties.getProperty. Typically the properties look like the above screen shot.
5. Add Custom Field by Program
long companyId = CompanyThreadLocal.getCompanyId(); try { Â Â Â Â Â ClassName className = ClassNameLocalServiceUtil.getClassName(User.class.getName()); Â Â Â Â Â ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className.getClassNameId()); Â Â Â Â Â Â Â Â Â Â Â ExpandoColumnLocalServiceUtil.addColumn(expandoTable.getTableId(), "custom-field1", ExpandoColumnConstants.STRING); Â Â Â Â Â Â Â Â Â Â Â } catch (SystemException e) { Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â } catch (PortalException e) { Â Â Â Â Â Â Â Â Â e.printStackTrace(); Â Â }
The above code adds a custom field with the name custom-field1 of type String for User Entity.
6. Update existing Custom Field Name
long companyId = CompanyThreadLocal.getCompanyId(); try { ClassName className = ClassNameLocalServiceUtil.getClassName(User.class.getName()); ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className.getClassNameId()); ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(expandoTable.getTableId(), "custom-field1"); ExpandoColumnLocalServiceUtil.updateColumn(expandoColumn.getColumnId(), "custom-field2", ExpandoColumnConstants.STRING); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); }
The above code will change a custom field (i.e., custom-field1) to custom-field2. Here we have changed only the name of existing custom field but not the value.
7. Delete Column
long companyId = CompanyThreadLocal.getCompanyId(); try { ClassName className = ClassNameLocalServiceUtil.getClassName(User.class.getName()); ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className.getClassNameId()); ExpandoColumnLocalServiceUtil.deleteColumn(expandoTable.getTableId(), "custom-field1"); } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); }
The above code will delelte the column custom-field1
8. Checking existance of a Column or Custom Field
long companyId = CompanyThreadLocal.getCompanyId(); try { ClassName className = ClassNameLocalServiceUtil.getClassName(User.class.getName()); ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, className.getClassNameId()); ExpandoColumn expandoColumn = ExpandoColumnLocalServiceUtil.getColumn(expandoTable.getTableId(), "custom-field1"); if(expandoColumn == null){ System.out.println("There are no custom field with name custom-field1 for the Entity User"); }else{ System.out.println("There are custom field with name custom-field1 for the Entity User"); } } catch (SystemException e) { e.printStackTrace(); } catch (PortalException e) { e.printStackTrace(); }
The above code check for the User Entity is there are any custom field or column with the name custom-field1.