Recursively expanding children in OneDrive graph API - microsoft-graph-api

I would like to get all the contents of a folder in OneDrive. The folder has child folders within it, and child folders within them, e.g.:
Parent/
└── Child1/
├── Child1.1/
│ ├── File1.1.1
│ └── File1.1.2
└── Child1.2/
├── File1.2.1
└── File1.2.2
I would like to maintain knowledge about folder hierarchy, basically something like Parent/Child1/File1.1.1 so I can't use the empty query graph search approach referenced here, which returns everything in a flat format without any information about the hierarchy.
I could recursively call each folder one by one but that would be a lot since there are thousands of folders in the parent directory.
Based on the docs (and here too), I would have expected the expand=children parameter to work, but it seems not to. Any ideas?
GET https://graph.microsoft.com/v1.0/me/drive/items/<id>/children?$expand=children
{
"error": {
"code": "notSupported",
"message": "Operation not supported",
"innerError": {
"date": "2022-12-17T17:03:28",
"request-id": "",
"client-request-id": ""
}
}
}

Related

How to correctly move a mail folder from one mailbox to another?

I am trying to move folders from a user mailbox to a shared mailbox using
graphClient.Users[completedMailbox].MailFolders[folderID].Move(fPrincipalId).Request().PostAsync();
note: the above was working for many years until recently.
So I am wondering if something has changed as other people are experiencing the same, see my other post about the issue here but no resolution.
Microsoft QnA - Mailbox move in progress. Try again later., The user and the mailbox are in different Active Directory sites
In the link above you can see I have tied directly on the Graph API Explorer
POST https://graph.microsoft.com/v1.0/users/office#w*g.com/mailFolders/AAiSjAAA=/move
{
"destinationId": "AA*WyUAAA="
}
which returns
{
"error": {
"code": "ErrorMailboxMoveInProgress",
"message": "Mailbox move in progress. Try again later., The user and the mailbox are in different Active Directory sites.",
"innerError": {
"date": "2021-05-20T16:14:04",
"request-id": "c5f45ede-c5ad-40da-8528-bbafa91dede5",
"client-request-id": "***"
}
}
}
Thanks!

Get all the files in others Onedrive in MS Graph

I am trying to list all files in another user's OneDrive.
I am calling GET /users/{user-id}/drive/items/{item-id}/children but my problem is that I do not know how to get the {item-id}.
I tried using https://graph.microsoft.com/v1.0/users/{userid}/drives but it is not showing any item ids.
Subsequently, once I get the id, I will use the copy command POST /users/{userId}/drive/items/{itemId}/copy to copy items.
If you do not know the ID or Path for the DriveItem, you'll need to start at the root of the Drive and walk the folder structure until your find the file(s) you're looking for.
Your first call would be to /users/{id|userPrincipalName}/drive/root/children?$select=id,name,folder,file. This will list the children's id, name, and either the folder or file data (depending on the item type) from the root folder:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('id')/drive/root/children(id,name,folder,file)",
"value": [
{
"#odata.etag": "\"{etag},1\"",
"id": "folderId",
"name": "My Folder",
"folder": {
"childCount": 10
}
},
{
"#odata.etag": "\"{etag},1\"",
"id": "fileId",
"name": "filename.ext",
"file": {
"mimeType": "type",
"hashes": {
"quickXorHash": "hash"
}
}
}
]
}
You then execute the same call against each of the folders to get their children, repeating the pattern until your find the file you're looking for:
/users/{id|userPrincipalName}/drive/items/{folderId}/children?$select=id,name,folder,file`
Alternatively, you could simply search for the file using /users/{id|userPrincipalName}/drive/root/search(q='{search-text}').
You probably need to query root to get childIDs for files/folders
GET https://graph.microsoft.com/v1.0/users/{user-id}/drive/root/children

Referencing json from a different file

Maybe I'm not quite understanding swagger quite yet (just did a tutorial). But I am trying to create documentation for an API we are building. Everything points to swagger, so decided to try that. Got it working pretty well. The issue I am having though is moving a schema from the swagger.json file to a seperate file to keep things clean.
The way the documentation has it, you give it a type and then $ref which I did. I then linked it to a file within a folder (of another folder) within the current directory.
{
...
"schema": {
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
...
}
}
}
}
Not exactly this but this gives a good idea of what I had working. If I have this in the same file, this works as I want it to. The issue I am having now is trying to move the properties to another file, as documented in the swagger documentation (maybe this is where I am misunderstanding).
So, I then take away the properties object, and replace it with:
"schema": {
"type": "object",
"$ref": "./components/schemas/staticData"
}
Here's what the external file looks like:
{
"properties": {
"data": {
...
}
}
}
When I run this, I get the following error:
Resolver error at paths./*/*.get.responses.200.schema.$ref
Could not resolve reference: undefined undefined (I obfuscated the path since it's irrelevant and I don't want our paths to be on a public website).
What I am looking for is how the external file needs to be structured so I can put these schemas in a different file and not clog up the root swagger.json file.
EDIT This is the folder structure
public
components
schemas
staticData.json
swagger.json
Thanks in advance.

Get user list with whom OneDrive Document is shared using Graph API

I am using Graph API to read OneDrive document successfully.
I have a requirement to get the list of users with whom the document is shared with.
The DriveItem does have the Shared property but it only tells whether is shared with user\organsization\anonymous, however it does give me list of users with whom I have shared the document.
As per documentation
The Shared resource indicates a DriveItem has been shared with others.
{
"owner": { "#odata.type": "microsoft.graph.identitySet" },
"scope": "anonymous | organization | users",
"sharedBy": { "#odata.type": "microsoft.graph.identitySet" },
"sharedDateTime": "datetime"
}
Is there a way to get shared with "users list" for a given document?
The API supports this via the Permissions resource. Permissions define the level of access provided (read,write,sp.owner, orsp.member`).
You can return a list of Permissions using the //permissions endpoint of a given file. For example, to /me/drive/items/{item-id}/permissions will return the permissions for the {item-id} in the current user's default Drive.

createLink not working as for file inside a read-only folder

We are using Microsoft Graph's /createLink API to share items. We are facing strange issues with the follow scenarios:
A File shared as read-only to an account which appears in /sharedWithMe results. We are able to call /createLink for this file.
A Folder shared as read-only to an account which appears in /sharedWithMe results. We are not able to call /createLink for any files inside that folder.
Error Received:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "xxxx-xxxx-xxxx-xxxx-xxxxx",
"date": "2017-11-07T12:54:50"
}
}
}
Our App is registered with Files.ReadWrite.All.
Why this difference in placing of files?

Resources