Microsoft365R icon indicating copy to clipboard operation
Microsoft365R copied to clipboard

Recursive List Items

Open wtimmerman-fitp opened this issue 2 years ago • 8 comments

Is there a way for list_items() to work recursively in directories? If not, this feature would be very handy to have.

First, to make sure I'm not misunderstanding how to use the package, I can run all of the following commands successfully:

site <- get_sharepoint_site(
  site_url = "https://[our subdomain].sharepoint.com"
)

drv <- site$get_drive()

drv$list_items()

drv$list_items(filter = "name eq '[name of some folder at top level of hierarchy]'")

But when i try to use filter with a name in one of the subfolders, doesn't seem to get results.

drv$list_items(filter = "name eq '[name of file in a subfolder with extension]'")
# [1] name  size  isdir id   
# <0 rows> (or 0-length row.names)

I've seen that there are some ways to do recursive operations by looking up child directories in for loops, but that seems like a non-starter for the volume of directories I need to iterate over, each of which with their own folder hierarchy.

My overall goal is to replicate something like the search feature on Sharepoint--I need to return a dataframe of document metadata for documents with a specific name scattered throughout our Sharepoint site.

wtimmerman-fitp avatar Mar 24 '22 14:03 wtimmerman-fitp

Right now there is no built-in support for recursive directory listing. You could write something up yourself where you check for directories in the returned list and call list_items() recursively.

hongooi73 avatar Mar 24 '22 16:03 hongooi73

Understood, thanks!

wtimmerman-fitp avatar Mar 24 '22 16:03 wtimmerman-fitp

recursive will be handly specially when you are deleting subfolders,

msgoussi avatar Mar 27 '22 04:03 msgoussi

@msgoussi recursive deleting of subfolders should already be handled automatically

hongooi73 avatar May 19 '22 18:05 hongooi73

I created a gist for a very basic approach for a recursive file and directory listing function using a for loop. https://gist.github.com/CBSti/ae0c2921e7986b517db3e6f3e3a4d0ba I have not tested this with larger hierarchies/directory structures, but possibly someone could find this useful.

CBSti avatar Jun 13 '23 09:06 CBSti

I also ended up writing something to do this

https://gist.github.com/jonocarroll/d5d315f1504d4cf8a06caf0bfdf58af5

This uses a depth-first search to recursively list the files and folders and doesn't have additional dependencies (base R). I use jsTree to interactively explore/search the results. I added a depth limit but this can be raised as required.

jonocarroll avatar Jun 13 '23 23:06 jonocarroll

I've started developing a package, {sharepointr}, to build out some "missing" features for {Microsoft365R} and added a function modeled on fs::dir_info() that includes both recursive item listings and a couple other user-friendly features.

Documentation is here: https://elipousson.github.io/sharepointr/reference/sp_dir_info.html

Code is here: https://github.com/elipousson/sharepointr/blob/HEAD/R/sp_dir.R

elipousson avatar Oct 11 '23 18:10 elipousson

@elipousson that looks incredible!

hongooi73 avatar Feb 14 '24 04:02 hongooi73