Dynamically determine Structure IDs in Liferay

Liferay Logo

When using multiple Web Content (aka JournalArticle) structures in Liferay managing their corresponding IDs via configuration files can become a tedious task. Luckily, there is an easy way to determine structure IDs dynamically based on their title, like the following code snippet demonstrates:

private static String STRUCTUREID = null;

private String getStructureId() {
  try {
    if (STRUCTUREID != null) {
      return STRUCTUREID; // already set
    }

    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
      JournalStructure.class).add(PropertyFactoryUtil.forName("name").like(
      "%\">Your Structure Title</>%"));
			
  @SuppressWarnings("unchecked") //optional
  List<JournalStructure> structures = JournalStructureLocalServiceUtil.
    dynamicQuery(dynamicQuery, 0, 1);

  if (structures.size() < 1) {
    System.out.println("No structure found");
    return null;
  }

  STRUCTUREID = structures.get(0).getStructureId();

  System.out.println("Structure ID=" + STRUCTUREID);

  return STRUCTUREID;

} catch (SystemException e) {
    e.printStackTrace();
    return null;
  }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.