Posted on Dec 16, 2013 By Philip Whitt


If you're integrating dotCMS with an existing system, more than likely you're going to need to associate data from other systems with a host or other structure. This is easy to do manually via the UI but being a programmer, you're probably going to want to automate this. 

One would assume that this functionality would be available via the StructureAPI and while this is true, navigating the StructureAPI is a bit awkward.  

To access a structure via the StructureAPI, it would look something like this:

Structure struct = StructureCache.getStructureByVelocityVarName("Host");

Structure struct = APILocator.getStructureAPI().find(struct.getInode(), APILocator.getUserAPI().getSystemUser());

The only real benefit I see to using the StructureAPI is for enforcing permissions. If your code to modify the structure is exposed to the public then using the StructureAPI is recommended. For private uses, using the object returned by StructureCache works just fine. 

Once you have the Structure, you can then add/modify fields on the structure.

Structure struct = StructureCache.getStructureByVelocityVarName("Host");

Field field = new Field();
field.setFieldType("text");
field.setFieldName("My Custom Variable");
field.setVelocityVarName("myVar");
field.setStructureInode(struct.getInode());

FieldFactory.saveField(field);



Back to Blog