Milesh
Milesh

Reputation: 271

How to get all childrens of folder in Alfresco?

I am trying to get all the children of folder (folder created in Alfresco server) using the following code

//Path is the path to the location of the folder in alfresco server
String folderId = session.getObjectByPath(path).getId();
Folder folder = (Folder) session.getObject(folderId);
if(folder.getChildren()!= null) {
  ItemIterable<CmisObject> childFolderIterable = folder.getChildren();
  if(childFolderIterable.getHasMoreItems()) {
    Iterator<?> childFolders = childFolderIterable.iterator();
    while(childFolders.hasNext()) {
      Folder subChildFolder = (Folder) childFolders.next();
      getChildrenRepositories(subChildFolder);              
    }
  }
}

Now my question is that folder at Alfresco contains 2 different sub-folders but the following code childFolderIterable.getHasMoreItems() returns false.

Can someone please look into this and guide me where I am wrong or is there any appropriate way to find the children of the folder?

Upvotes: 1

Views: 4761

Answers (2)

Milesh
Milesh

Reputation: 271

Also it can be done in the following way:

 for(CmisObject obj: folder.getChildren()) {
        System.out.println("Name: " + obj.getName());
        System.out.println("Id: " + obj.getId());
        System.out.println("Type: " + getType(obj.getType()));
        System.out.println();
    }

Upvotes: 4

gclaussn
gclaussn

Reputation: 1786

The getHasMoreItems() method documentation says:

Returns whether the repository contains additional items beyond the page of items already fetched. Returns: true => further page requests will be made to the repository

If it returns false, it means that the page you fetched by calling getChildren() contains already all children of this folder and no further requests (to get the remaining children) are required. If you have a folder with a lot of children you can use the operation context to page the results. The OpenCMIS project site provides an appropriate example.

If you use no paging, then simply call:

Iterator<CmisObject> it = folder.getChildren().iterator();

while(it.hasNext()) {
  CmisObject object = it.next();

  // Do something with the child object
}

The iterator should provide the two children you are looking for.

Upvotes: 3

Related Questions