It's all about the answers!

Ask a question

How can I get a list of the root build folders of a project area from API?

Arne Bister (2.6k12832) | asked May 11 '15, 10:15 a.m.
trying to retrieve a list of the root build folders from a given project area in order to script build definition creation and moving the build definition into the correct folder via script as well.

What I found so far only returns only a single folder, however (the first one I created, regardless on what level it is now). I also only found an IBuildFolderEntryQueryModel rather than an IBuildFolderQueryModel so trying this code:

        IBuildFolderEntryQueryModel bfQueryModel = IBuildFolderEntryQueryModel.ROOT;        
        try {
            IItemQuery itemQuery = IItemQuery.FACTORY.newInstance(bfQueryModel);
            IItemQueryPage itemPage = ClientFactory.getTeamBuildClient(repo).queryItems(itemQuery, IQueryService.EMPTY_PARAMETERS, IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, null);
            buildFolderEntryHandles = itemPage.getItemHandles();
            for (IBuildFolderEntryHandle bfeHandle : buildFolderEntryHandles) {
                IBuildFolderEntry buildFolderEntry = (IBuildFolderEntry) repo.itemManager().fetchCompleteItem(bfeHandle, IItemManager.DEFAULT, null);
                IBuildFolderHandle buildFolderHandle = buildFolderEntry.getFolder();
                IBuildFolder buildFolder = (IBuildFolder) repo.itemManager().fetchCompleteItem(buildFolderHandle, IItemManager.DEFAULT, null);
                System.out.println("Found folder: "+buildFolder.getName());
        } catch (TeamRepositoryException e) {

Any better ideas on how to achieve this? Actually if there is a method such as BuildWhateverClient.getBuildFolderByPath() I would gladly settle for that but I am willing to implement it if I could just get a complete list of root folders.

Accepted answer

permanent link
Krishna Kishore (50112) | answered May 12 '15, 4:53 a.m.
edited May 12 '15, 4:53 a.m.
 Hi Arne,

You can use TeamBuildClient.getChildrenOfFolder call to get the folders. 

public IBuildItemNamePair[] getChildrenOfFolder(final IBuildFolderHandle buildViewFolderHandle, final IProcessAreaHandle[] processAreaHandles, final IProgressMonitor progressMonitor)

The first parameter to the call is the handle to the parent folder, it this is null then the top level folders are returned.

If you want to use the query model directly, some think like the below code snippet should work

IBuildFolderHandle buildFolderHandle;// either null for top level folders or handle of the parent folder 
IProcessAreaHandle[] processAreaHandles; // process area handles to retrieve the folders for.

BuildFolderQueryModel buildFolderQueryModel = BuildFolderQueryModel.ROOT;

//create instance of the query
IItemQuery folderQuery = IItemQuery.FACTORY.newInstance(buildFolderQueryModel);

// First predicate: the process area of the folder must be in processAreaHandles
List queryParameters = new ArrayList<IItemHandle>(processAreaHandles.length);
IPredicate processAreas = QueryPredicateHelper.getProcessAreaInPredicate(folderQuery, buildFolderQueryModel.processArea(), processAreaHandles, queryParameters);
// Second predicate: the parent of the folder must be buildFolderHandle (or null if top level folders)
IPredicate parent;
if (buildFolderHandle == null) {
    //top level folders
    parent = buildFolderQueryModel.parent()._isNull();
} else {
    //child folders
    parent = buildFolderQueryModel.parent()._eq(folderQuery.newItemHandleArg());

//apply the filter on the process areas

//get the folder items
IItem[] folderItems = queryHelper.queryItems(folderQuery, queryParameters.toArray());


Arne Bister selected this answer as the correct answer

Arne Bister commented May 12 '15, 5:02 a.m.

Great, thanks!

Your answer

Register or to post your answer.