Changing directory into a shared folder via Files API

A user’s folder can easily be retrieved with
$currentDirectory = $this->rootFolder->getUserFolder($user->getUID())
after IRootFolder and IUserSession have been properly injected as dependencies.

A listing of the folder’s contents
will show the user’s files and folders as well as shared folders which the admin has made accessible for that user.

Now, changing the working directory from there can be done via
$subDirectory = $currentDirectory->search("myFolderName")[0].

However, the search() method appears to work only with a normal user folder but not with a shared folder. In the latter case it returns an array of size 0, content null, end of story.

I managed to gain access to a shared folder by using the uid:
$sharedFoldersContent = $currentDirectory->getById(123)[0]->getDirectoryListing();

But this means that one would have to get the user’s directory, create a listing, loop over its items until its name matches the sought one, get this item’s ID and only then be able to access its contents. This surely is not the recommended way, is it?

Note: once inside the shared folder, search() works again.

OK, please note first that you are not changing the directory by invoking search or similar functions. All you do is traversing the file system and appending paths.

If you know the name of the file/folder you are looking for beforehand, you will not need to do a omplete traversal and then filter out the expected one. You can plainly use the get method instead. This should also work with complete relative paths (.get('foo/bar/baz') instead of .get('foo').get('bar').get('baz')) if I am not completely mislead. See also the API spec on the Folder class.

You did not mention your exact use-case, so this is a best-effort to answer your question. Feel free to narrow down your question.

Thanks for pointing me to the get() method which will solve my objective best, so my case is closed.

On the other hand this does little in the way of explaning the odd behaviour of the search() method. I will leave this here for others to find :slight_smile: : just don’t do a search() on shared folders in top level, it will find nothing.