How to efficiently create a sub-folder

I would like to create a sub-folder in an arbitrarily nested directory efficiently using the default Platform API.

Based on my understanding I always have to provide the parent directory id but I don’t know how to query for it efficiently. By name only is not possible because i can have multiple folders with the same name in different directories. I cannot query on “path” either, this always would give me an error message.

One possible approach is:

  • Take the last segment of the parent’s path → “folder”
  • run a search query with name equal to this “folder”
    ** if you get one or more results, then look up the “path” attribute and compare if it matches the remaining parent’s path-part
    *** if you have a match then this is your parent folder
    ** if you get no result, then create the parents folder, e.g. by recursive call to the create folder method.

This potentially results in quite some api calls. Worst case, number of calls is the number of nested levels * 2.

Is there a better approach?

Please explain the whole usecase. Right now, it sounds like you are creating the whole structure, so you should already be aware of the tree. How comes it to the need to query it? What are you storing? Might be worth to flatten it?