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.
Related
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.
I have a SPO site within a SPO site collection.
My site has a DocumentLibrary and inside it 3 folders (Alfa, Beta and Gamma): I would like to get get the ID of one of them (for example Beta) by Graph API.
I can get all of them by writing:
https://graph.microsoft.com/v1.0/sites/[id site collection]/sites/[id site]/drives/
I can select a specific fields:
https://graph.microsoft.com/v1.0/sites/[id site collection]/sites/[id site]/drives?$select=name,id
but I can't filter the results, the following requests return always the same results of the first one:
https://graph.microsoft.com/v1.0/sites/[id site collection]/sites/[id site]/drives?$filter=name eq 'Beta'
https://graph.microsoft.com/v1.0/sites/[id site collection]/sites/[id site]/drives?filter=name eq 'Beta'
. Does anyone know why and explain me how to filter?
Thanks in advance.
As of now you cannot use ODATA query parameter $filter to filter the document libraries as it is documented that it can support only few query parameters. There is already a feature request raised in Microsoft Graph Feedback Forum, please upvote it so that the product team may develop it in future.
For now you should query it on your end(Client side) using list drives and pick the required drive object by the name property from drive objects listed.
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?
I'm trying to keep a synced list of files in a Sharepoint group by calling:
/groups/{groupId}/drive/root/delta
When there is no token submitted in the request, it should return the current state, but some files are missing. I'm looping all pages by following odata.nextlink, but I only get 3102 elements, compared to 3134 elements in Sharepoint (exported to Excel). There seems to be no logic in the files missing
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