folder.getSheets() returning null?

345 views Asked by At

I'm trying to go through and entire workspace and list each element in a treeview, but when I attempt to grab the sheets in the found folders the getSheets() method in the Folder class always returns null. Am I doing something wrong?

    TreeItem<String> workspace = new TreeItem<>("Earls Workspace", workspaceIcon);
    workspace.setExpanded(true);

    try {
        Workspace earlsWorkspace = SmartsheetConnector.getInstance().getSmartsheet().workspaces().getWorkspace(*****);
        List<Sheet> sheets = earlsWorkspace.getSheets();
        List<Folder> folders = earlsWorkspace.getFolders();

        for (Folder folder : folders) {
            TreeItem<String> item = new TreeItem<>(folder.getName(), new ImageView(folderIcon));
            System.out.println("Folder: " + folder.getName());
            List<Sheet> folderSheets = folder.getSheets(); <- problem is here
            System.out.println(folderSheets.toArray());
            for (Sheet sheet : folderSheets) {
                TreeItem<String> subItem = new TreeItem<>(sheet.getName(), new ImageView(sheetIcon));
                item.getChildren().add(subItem);
            }

            workspace.getChildren().add(item);
        }

        for (Sheet sheet : sheets) {
            TreeItem<String> item = new TreeItem<>(sheet.getName(), new ImageView(sheetIcon));
            workspace.getChildren().add(item);
        }

        sheetExplorer.setRoot(workspace);
    } catch (SmartsheetException ex) {
        ex.printStackTrace();
    }

I've purposefully blocked out the workspace ID.

1

There are 1 answers

0
arhajiismael On

Since the Java SDK mimics the Smartsheet API, the GET workspace by ID function only returns high level folder information, and does not include the sheets within the folder, which is why it is returning null. There are two ways to solve your problem:

  1. After getting a list of folders from your workspace, you can call the GET folder by ID function, and that will return a list of sheets within a folder.

    Workspace earlsWorkspace = SmartsheetConnector.getInstance().getSmartsheet().workspaces().getWorkspace(*****);
    List<Folder> folders = earlsWorkspace.getFolders();
    
    for (Folder folder : folders) {
        List<Sheet> folderSheets = SmartsheetConnector.getInstance().getSmartsheet().folders().getFolder(folder.getId()).getSheets();
    
        for (Sheet sheet : folderSheets) {
            System.out.println(sheet.getName());
        }
    }
    
  2. Call the getHome() function. This will return a nested list of all Home objects, including sheets, workspaces and folders, and optionally reports and/or templates depending on the EnumSet that you pass in. You will have to go through the list of workspaces and find the one that you are interested in. From there, you can loop through the folders, and call folder.getSheets() to get a list of sheets within a folder.

    Home home = SmartsheetConnector.getInstance().getSmartsheet().home().getHome(EnumSet.of(ObjectInclusion.TEMPLATES);
    

In the next release of the SDK, there will be an option to specify if you want to load all of the contents, including nested folders and nested sheets within a workspace, so you can easily call folder.getSheets(), just like you were doing, and get back a list of sheets.