The right way to list user folders with DataManagement API

Forge Community Blog

Information. Insights. Code. Connections.

Blog
The right way to list user folders with DataManagement API

The right way to list user folders with DataManagement API

April 6, 2017

Here is a mistake I was doing when trying to list folders under a project of an authenticated user with the Forge DataManagement API:

  • getting acces to the project details
  • grabbing the root folder id
  • ... and listing the content of this folder.

This approach works only as long as the user is an admin of the project, so I could not see the issue when testing with my account.

The code looked as follow:

this.dmAPI.getProject(
  node.hubId, node.projectId).then((project) => {

      const rootId = project.data.relationships.rootFolder.data.id

      this.dmAPI.getFolderContent(
         node.projectId, rootId).then((folderItemsRes) => {

      //list all folder items
  })

However for an other user who has been granted access to a project or specific folder without being an admin, this approach is invalid. The righ way to do it is to use a specific endpoint which was added a bit later: GET hubs/:hub_id/projects/:project_id/topFolders

As mentioned in the documentation, this endpoint

Returns the details of the highest level folders the user has access to for a given project. The user must have at least read access to the folders.

If the user has access to the project’s root folder, it only returns details of the root folder. If the user does not have access to the root folder, it returns details of all the highest level folders in the folder hierarchy the user has access to.

Note that when users have access to a folder, access is automatically granted to its subfolders.

The correct code now looks as follow, where I wrapped the REST request into a custom API object:

this.dmAPI.getProjectTopFolders(
  node.hubId, node.projectId).then((folderItemsRes) => {

  //list folder items recursively ...
})

This is just a very shortened version of the actual code that runs in my demo app. The complete code can load asynchronously and recursively all folders, subfolders and items under each user account hubs and displays them in a treeview. This is some of the most complex asynchronous code I had to write ... You can take a look at it there

The full project code is located there and the live version runs at https://dm.autodesk.io

 

Philippe Leefsma
Cloud Fellow

I write JavaScript for the future 3D World Wide Web, A.K.A Forge Platform