In Liferay, you might need to count the number of web content articles associated with a specific structure ID. This guide provides a step-by-step method using a Groovy script.
Counting Web Content Articles by Structure ID
The following Groovy script counts the number of web content articles associated with a specific structure ID:
import com.liferay.journal.service.JournalArticleLocalServiceUtil
// Replace 12345 with your structure ID
long structureId = 12345L
// Fetch all journal articles
List articles = JournalArticleLocalServiceUtil.getJournalArticles(-1, -1)
// Filter articles by structure ID and count them
int count = articles.count { it.getDDMStructureId() == structureId }
println "Number of web content articles with structure ID ${structureId}: ${count}"
Steps to Execute
- Go to Control Panel > Server Administration > Script.
- Paste the above script into the console.
- Click Execute.
- The output will display the total number of articles with the specified structure ID.
Best Practices and Warnings
- Test in Development: Always test scripts in a development or staging environment before running them in production.
- Restrict Access: Limit access to the Script Console to trusted administrators only.
Also Read: How to Fetch a Category Name Using Category ID in Liferay with FreeMarker