I have a requirement that, Need to create JournalArticle with Structure and Template.While creating JournalArticle the method expecting the StructureId and TemplateId but these are generated by Liferay.So by name how can i get Id's of both.
Asked
Active
Viewed 3,477 times
2
-
One way around is to fetch all structures and templates, iterate through them and populate required structure / template based on name check. Other way is to pass `DynamicQuery` to `xxxLocalServiceUtil`'s method `dynamicQuery`, which will return you list of results. – Parkash Kumar Apr 05 '16 at 08:34
-
1.[***Fetch All***](http://stackoverflow.com/questions/25603742/get-content-of-structure-liferay) 2.[***Dynamic Query***](https://www.liferay.com/community/wiki/-/wiki/Main/Queries+2%3A+DynamicQuery+API) – Parkash Kumar Apr 05 '16 at 08:41
-
1No different..@Romeo – Kona Laxman Deepak Apr 05 '16 at 10:35
2 Answers
4
Create and execute a DynamicQuery
, like so (just replace Template
with Structure
to get structures):
DynamicQuery q = DynamicQueryFactoryUtil.forClass(DDMTemplate.class)
.add(PropertyFactoryUtil.forName("name").like("%YOUR NAME%"));
List<DDMTemplate> templates = DDMTemplateLocalServiceUtil.dynamicQuery(q);
You have to use like
since the names of the structures/templates are saved like so:
<?xml version='1.0' encoding='UTF-8'?>
<root available-locales="de_DE" default-locale="de_DE">
<Name language-id="de_DE">YOUR NAME</Name>
</root>
There can be different names for different locales.

Timo Türschmann
- 4,388
- 1
- 22
- 30
-
1Hi, just a small improvement suggestion: In order to avoid selecting structures whose name just _starts_ with "YOUR NAME", you should better change the code `.like("%YOUR NAME%")` to `.like("%>YOUR NAME<%")`. – Petr Bodnár Aug 16 '17 at 09:22
1
You can get StructureId (called DDMStructure) with this code
long classNameIdJournalArticle = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class);
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(groupId, classNameIdJournalArticle, "myDDMStructureName");
And TemplateId (called DDMTemplate) with this code
DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(groupId, classNameIdDDMStructure, "ddmTemplateName");

fabballe
- 761
- 4
- 12
-
2
-
In fact, the structureKey / templateKey _could_ be considered as a name. When you look into the "ddmstructure" table, you will even find regular names there for some _predefined_ structures (instead of generated numbers). Unfortunately, Liferay API is just not clear about this, you won't probably find any documentation on this as well. All that one knows for sure, you can't change these "*Key" values via the GUI... – Petr Bodnár Aug 16 '17 at 09:36