List all files recursively in microsoft graph - microsoft-graph-api

We are trying to retrieve all files under a folder recursively, but I can't find an API for that.
I've tried using the search API without a query, but this doesn't return anything.
https://graph.microsoft.com/v1.0/sites/root/drive/root/search(q='{}')
Is this possible in microsoft graph ?

It turns out the correct link is:
https://graph.microsoft.com/v1.0/sites/root/drive/root/search(q='')
Thanks #Marc LaFleur
2021 edit: this api unfortunately doesn't return all the files. So I ended up using the sharepoint delta api to sync sharepoint data locally, then I use a mysql query to find all files of a given folder (there is a child <-> parent relation in the files/folder metadata return from the API)

If you want to find all of the files recursively of a SharePoint Document Library (which is still a Drive), you can apply a filter on the Content type not being a Folder. for example:
https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items?$expand=fields,driveItem&$filter=fields/ContentType eq 'Document'
This will return all of the Documents (i.e.: anything not a folder) recursively. Since the driveItem is being expanded, you will have all of the DriveItem details needed.

I am able to list all items with this below :
{drive-root-id} is the /Drive/root "id" value return from :
https://graph.microsoft.com/v1.0/{site-id}/drive/root
Then :
https://graph.microsoft.com/v1.0/{site-id}/drive/items/{drive-root-id}/children

Related

How can i retrieve all directories and sub directories in OneDrive using Graph Api?

I have OneDrive with a number of folders and sub folders .
So I have a root directory with folders as follows
.Main
.Folder1
.Folder1a
.Folder1b
.Folder1b1
.UserDetails
.TextFile.txt
So there are 3 parent folders Main, Folder1 and UserDetails and Folder1 has sub folders Folder1a and Folder1b and Folder1b has sub folder Folder1b1.
I have made a request to:
https://graph.microsoft.com/v1.0/me/drive/root/children
and try to traverse the result to retrieve the sub directories.
So in the response the list of the folders and the folders inside the folders .
Is there one call i can make that can give me back this data in a Json result set ?
From my understand of your question the desired behavior is for the response to return an object representation of your OneDrive contents.
MSGraph doesn't yet support this functionality, you would have to retrieve sub-folders per folder.
Based on this documentation, you can use the drive-id to make the following request to list the children:
GET https://graph.microsoft.com/v1.0/drives/{drive-id}/items/root/children
Or if you want to list the items in a sub-folder:
GET https://graph.microsoft.com/v1.0/drives/{drive-id}/items/{item-id}/children
To list the children based on the Path:
GET https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/{path-relative-to-root}:/children
The other way you can go about this, is to write a recursive function on the client side to fetch your oneDrive JSON object.

Getting more than 1000 documents using folder() in Appian

I am writing an Appian web API, to retrieve documents from our Appian system which will be used to integrate with our other systems.
To this end, I am using the folder() method to get information about the contents of a folder in Appian.
folder(
theCaseFolder,
"documentChildren"
)
The problem I am having is that while this code works most of the time - we have some cases where there are more than 1000 documents stored against the case. I note that the Appian documentation states that:
The documentChildren and folderChildren properties return up to the first 1000 documents or folders, respectively, that are direct children of the selected folder.
My problem is that we have a few cases where there are more than 3000 documents attached to the case. Is there a way to get a list of of those child documents, or am I plain out of luck?
In long term I would suggest storing some information about document in separate table in db. In this way you can query db as you wish by Appian or by SQL.
In short term you can get first 1000 as it is in documentation and then move them to subfolder/different folder or delete. This can be repeated multiple times to get all files from folder.
Move Document Appian Function

Microsoft Graph list children including grandchildren

Is there a way to recursively include the content of the children (grandchild) in one request in the Microsoft Graph API?
I want to query folder /foo/ and also get the content of /foo/baar/ e.g /foo/baar/baz.txt.
IGraphServiceClient graphClient = GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.buildClient();
IDriveItemCollectionPage children = graphClient
.drives("{drive-id}")
.items("{item-id}")
.children()
.buildRequest()
.expand("children")
.get();
Expanding the query using children returns a com.microsoft.graph.http.GraphFatalServiceException: Unexpected exception returned from the service.Error code: notSupported
This would significantly enhance the performance of my requests.
Edit: just created a feature request: List children including grandchildren
It is not possible with expand to get files more than 1 level. It is mentioned in Known Issues section of Microsoft Graph Api docs.
But we can get all files and folders recursively using search.
Search with q=''(empty query search), this will return all files and folder in the search scope. For scope specific search, refer to this answer. You can use parentReference key to sort out which file belongs to which folder.
Example 1: The following query retrieves all files and folders in the root scope
https://graph.microsoft.com/v1.0/me/drive/root/search(q='')?$select=name,id,parentReference
Example 2: To search in the folder named 'temp' located in root folder
https://graph.microsoft.com/v1.0/me/drive/root:/temp:/search(q='doc')?$select=name,id,parentReference
Limitations with search (as of now)
Files and folders that are created recently take some time to reflect in the search results as they need to be indexed. They are visible when you query for children but they won't appear in search results.
The empty query trick works only with OneDrive Business accounts but not with OneDrive Personal accounts. For OneDrive personal accounts, when you search with empty query, it returns an error message Search Query cannot be empty"
The scope specific search(eg: search within a specific folder) works with OneDrive Personal but not with OneDrive business accounts.

Is it possible to select all files (files only) in a location using the Graph API?

I'm attempting to use the Graph API to select all folders and files for a SharePoint location.
I can use the following call to get a list of all driveItems (folders and files) in a location:
/drives/{drive-id}/root/children
I can extend this call to make it so it returns only items with the "folder" facet, meaning I can select all driveItems that are folders in this location:
drives/{drive-id}/root/children?$filter=folder ne null
What I want now is to be able to extend the original call so that it selects all driveItems with the "file" facet only. e.g. something like:
/drives/{drive-id}/root/children?$filter=file ne null
However, my experience with the Graph explorer suggests that nothing is supported that will allow me to do this.
Can anyone tell me any possible method for doing this? Hopefully using some OData parameters on /drives rather than using sites/(siteId)/lists(listId)/items - if this method were to be used, how then could i filter out Document Sets from the results?

Google Drive queryForFilesList not returning any results

I'm currently having issues with the iOS Google Drive SDK. I'm using GTLQueryDrive queryForFilesList to search for a file in my Google Drive. All the files I want have a path in the format directory-name/file-name. Since the SDK/API doesn't allow searching for files using a full path, I'm using the following query to ultimately get it's downloadUrl. I'm using a query in the following format:
((title = 'directory-name') AND ('root' in parents) AND (mimeType = 'application/vnd.google-apps.folder')) OR
((title = 'file-name') AND (not 'root' in parents) AND (mimeType != 'application/vnd.google-apps.folder'))
The first line is meant to find all directories in the root directory whose name matches mine, and the second line should match all files with the same name. This should return the directory i'm looking for, the file i'm looking for, and maybe some other stuff (e.g. files with the same name in other directories). I have some code to figure out which file is the correct one.
The problem I'm having is that sometimes I get no results from the query. This generally happens after I rename the file, and rename it back, or other things like that. The weird part is that if I run either of the two lines of the query independently, it returns correctly, but together they don't.
Any help would be greatly appreciated, and I would gladly provide more information if required.
And yes, I'm using the kGTLAuthScopeDrive scope.
The ideal solution would be if I could just search using a full path, so if there's a way to do this, then I'm not aware.
Unfortunately, I was unable to get this to work. And also unfortunately, Google does not provide an API to query for a full path. So I resorted to iterating over the path to get the directory IDs, and then when I get to the file, get its ID and download it. Although slightly more complex, my solution was based on this question and chosen answer: What's the right way to find files by "full path" in Google Drive API v2

Resources