Create page in aem by reading given paths from excell | Community
Skip to main content
Level 2
January 1, 2023
Solved

Create page in aem by reading given paths from excell

  • January 1, 2023
  • 1 reply
  • 1515 views

please give me a solution for creating pages in AEM by reading given paths from excel

PLEASE HELP ME WITH THE CODE 

excell should be like this

reading path from excell and then it will create pages automatically in AEM

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Monendra_Singh

Hi,

 

Here is an example of how you can read the paths from an Excel file and create pages in AEM using the Java Content Repository (JCR) API:

 

import javax.jcr.Session; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.PathNotFoundException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class PageCreator { private Session session; public PageCreator(Session session) { this.session = session; } public void createPagesFromExcel(String excelFilePath) throws IOException, RepositoryException { // Open the Excel file FileInputStream file = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(file); // Iterate through each sheet in the workbook for (Sheet sheet : workbook) { // Iterate through each row in the sheet for (Row row : sheet) { // Get the first cell in the row (should contain the path) Cell cell = row.getCell(0); String path = cell.getStringCellValue(); // Check if the path exists in the JCR if (!session.nodeExists(path)) { // Create the page Node page = createPage(path); } } } } private Node createPage(String path) throws PathNotFoundException, RepositoryException { // Get the parent node of the page String parentPath = getParentPath(path); Node parentNode = session.getNode(parentPath); // Get the name of the page String pageName = getPageName(path); // Create the page node Node page = parentNode.addNode(pageName, "cq:Page"); // Save the changes session.save(); return page; } private String getParentPath(String path) { int lastSlashIndex = path.lastIndexOf("/"); return path.substring(0, lastSlashIndex); } private String getPageName(String path) { int lastSlashIndex = path.lastIndexOf("/"); return path.substring(lastSlashIndex + 1); } }

 

 Note that this code is just a sample and is not intended to be used as is. You will need to modify it to fit your specific requirements.

To use this class, you will need to create an instance of it and pass in a JCR session. Then, you can call the createPagesFromExcel method, passing in the path to the Excel file containing the page paths.

This example assumes that the Excel file has a single sheet with a column of page paths(you got one additional column which you can change the code for). It will iterate through each row in the sheet, and create a page at the specified path if it does not already exist.

 

Thanks,

Monendra

1 reply

Monendra_Singh
Monendra_SinghAccepted solution
Level 3
January 1, 2023

Hi,

 

Here is an example of how you can read the paths from an Excel file and create pages in AEM using the Java Content Repository (JCR) API:

 

import javax.jcr.Session; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.PathNotFoundException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class PageCreator { private Session session; public PageCreator(Session session) { this.session = session; } public void createPagesFromExcel(String excelFilePath) throws IOException, RepositoryException { // Open the Excel file FileInputStream file = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(file); // Iterate through each sheet in the workbook for (Sheet sheet : workbook) { // Iterate through each row in the sheet for (Row row : sheet) { // Get the first cell in the row (should contain the path) Cell cell = row.getCell(0); String path = cell.getStringCellValue(); // Check if the path exists in the JCR if (!session.nodeExists(path)) { // Create the page Node page = createPage(path); } } } } private Node createPage(String path) throws PathNotFoundException, RepositoryException { // Get the parent node of the page String parentPath = getParentPath(path); Node parentNode = session.getNode(parentPath); // Get the name of the page String pageName = getPageName(path); // Create the page node Node page = parentNode.addNode(pageName, "cq:Page"); // Save the changes session.save(); return page; } private String getParentPath(String path) { int lastSlashIndex = path.lastIndexOf("/"); return path.substring(0, lastSlashIndex); } private String getPageName(String path) { int lastSlashIndex = path.lastIndexOf("/"); return path.substring(lastSlashIndex + 1); } }

 

 Note that this code is just a sample and is not intended to be used as is. You will need to modify it to fit your specific requirements.

To use this class, you will need to create an instance of it and pass in a JCR session. Then, you can call the createPagesFromExcel method, passing in the path to the Excel file containing the page paths.

This example assumes that the Excel file has a single sheet with a column of page paths(you got one additional column which you can change the code for). It will iterate through each row in the sheet, and create a page at the specified path if it does not already exist.

 

Thanks,

Monendra