My app is using client_credentials, and is successfully consuming the Graph API on most calls.
However, I have been attempting to get images from a sharepoint folder by path and filename:
https://graph.microsoft.com/v1.0/drive/root:/sites/folder1/folder2/folder3/folder4/photo.jpg
The appropriate headers are being set (authorization and accept).
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + session["access_token"]);
httpClient.DefaultRequestHeaders.Add("accept", "application/json; odata.metadata=minimal");
The request returns a 404 - Not found response.
When I navigate to our sharepoint site, I can see the image fine so I can only assume the file path is correct:
https://ourcompany.sharepoint.com/sites/folder1/folder2/folder3/folder4/photo.jpg
Apologies, but I am new to Graph API and couldn't find anything referencing this in the documentation:
https://learn.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0
Path
/me/drive/root:/path/to/file
Resource
Access a DriveItem by path relative to the user's OneDrive root folder.
Are there special permissions needed for this? I found something suggesting permissions might be required in azure portal but this is to do with azure active directory and I'm not sure if it will affect the graph API, as it was working OK, reading drives and drive items in sharepoint (by drive id and drive item id, not by file path).
EDIT: Went into azure portal, added Graph API permissions to application:
Site: ReadAll
Directory: ReadAll
File: ReadAll
No change so far.
A site contains the drive so you're referencing things in the wrong order. To get a DriveItem from a site's drive, you want this:
/sites/{siteId}/drive/root:/folder1/folder2/folder3/folder4/photo.jpg
If you're looking to download the DriveItem instead of just retrieving the metadata, you'll want this:
/sites/{siteId}/drive/root:/folder1/folder2/folder3/folder4/photo.jpg:/content
Related
We have to access several OneDrive files using Microsoft graph API. This works well until we met some file whose name ends with .svc (they are regular files).
We have a request that looks like:
https://graph.microsoft.com/v1.0/users/account#xxx.onmicrosoft.com/drive/root:/file.svc
And it fails with a 400 Bad request (all other requests succeed).
We have tried the following, without success:
Escape the dot . with %2E
I’ve seen that the .svc extension was stripped, so doubled it, the file name becomes file.svc.svc
How to encode a file name with a .svc extension so graph API accepts it? (There is a way for sure, since we managed to upload such a file)
To access file content itself, the URI had to be https://graph.microsoft.com/v1.0/users/account#xxx.onmicrosoft.com/drive/root:/file.svc:/content (the :/content at the end does the trick, because it is semantically correct).
How do I find a file that was deleted and had a relative path of /foo/baz.txt?
I am using the /v1.0 endpoint and my app requests the Files.ReadWrite.AppFolder scope. I can access /drive/special/appRoot.
Will /drive/special/appRoot:/search(q='baz.txt') find deleted files?
Will /drive/special/appRoot:/foo/baz.txt:/versionsversions for a deleted file?
There is not a lot of examples (or documentation support) for using AppFolder.
I'm afraid this isn't possible today.
When a file is deleted from OneDrive (either via the Web App or the API), it is sent to the Recycle Bin and held for 30 days (assuming the user doesn't manually empty it of course).
At the moment, it isn't possible to access or restore a DriveItem from a dive's Recycle Bin (this has been discussed but I'm unaware of any ETA). The only way to restore an item today is for the user to do so via the OneDrive Web App.
According to your description, I suppose you want to get a file that was deleted in App Folder.
Based on my test, we can use the following API to get the file in App Folder:
/drive/special/approot:/foo/baz.txt:/
However, if we deleted the file, it will return a 404 status code when we use this API.
We can get the file by using this API unless we restore it in the Recycle bin of OneDrive.
I try to search for files on SharePoint Document Library (e.g. the default 'root'). I created a few test-files by uploading them or create new Office files online and made some search-requests, e.g. https://graph.microsoft.com/v1.0/sites/root/drive/root/search(q='{query}') and until yesterday everything worked fine.
Now I started to edit files on SharePoint or created/uploaded new ones and with this edited or new files, I have the problem that I get no result when I search for them. "old" files, I created when I started I find although, as long as I don't edit them.
To get access I registered an App inside the AAD and gave it the needed permissions (
Sites.Read.All, Sites.ReadWrite.All, Files.Read.All, Files.ReadWrite.All
and a direct access to a specific file with https://graph.microsoft.com/v1.0/sites/root/drive/items/{item-id}/ works also well.
Search will read data from indexed data, but crawling and re-indexing of a library need to take some time. So you the code return null for the new files:
https://graph.microsoft.com/v1.0/sites/root/drive/root/search(q='{query}')
The following code get the library data directly but not based on the indexed data, so it works well.
https://graph.microsoft.com/v1.0/sites/root/drive/items/{item-id}/
How do I upload files to the sharepoint shared documents folder using Microsoft Graph Client. I dont see any examples in the API documentation for the same. Is it possible to achieve the same via the Graph client API
The following works
Stream filecontents = new MemoryStream(filedata);
await client.Drive.Root.ItemWithPath(NameOfFile).Content.Request().PutAsync<DriveItem>(filecontents);
If you don't insist on upload file to SharePoint Shared Folder, you can use the OneDrive SDK to implement your requirement(Or MS Graph OneDrive API).
If you insist on upload file to SharePoint but not OneDrive(although the OneDrive is a particular SharePoint site),you can see the following answer:
We can use the following API to operate the Documents Folder:
Access the default drive (document library) for the given site.
/sites/{site-id}/drive
Enumerate the drives (document libraries) under the site.
/sites/{site-id}/drives
You can try the following API to upload files:
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
More infomation:
https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/driveitem_put_content
https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/sharepoint
If above solution does not work on your side, then the result will be we cannot upload files to SharePoint Shared Documents folder directly by MS Graph Client now. Currently, Microsoft just support limited common feature for the following:List/Site/ListItem.
If the API doesn't work on your side, you may submit a feature request to the Product group:https://officespdev.uservoice.com/
I try to make integration with OneDrive, using the API https://dev.onedrive.com/getting-started.htm
I make auth work and i can get list of drives in the account .
When i execute the API call /me/drives i am getting the list of drives fine.
But i can not find how to get list of folders on a drive, subfolders of a folder etc.
From the docs i can see there are some API for this, but it doesn't work for me.
https://dev.onedrive.com/items/list.htm
I have to execute on of.
GET /drive/items/{item-id}/children
GET /drive/root:/{item-path}:/children
But it doesn't work. I have a Drive ID from a list of drives. When i execute
GET /drive/items/DRIVEID/children
i have error "Access denied. You do not have permission to perform this action or access this resource."
How to do this operation? should i use a drive ID or name? maybe the url is wrong, what must be the correct url if i have a drive name, ID ?
I have found how to do this. To understand i had to install some other tool where OneDrive API is used and debugged traffic with a https sniffer.
So, if endpoint is https://graph.microsoft.com/v1.0/me/
then paths are
/drives/DRIVEID/root/children
for root of drive
/drives/DRIVEID/root:myfolder/subfolder:/children
for a folder myfolder/subfolder
Final url is like
https://graph.microsoft.com/v1.0/me/drives/DRIVEID/root:myfolder/subfolder:/children
/drive is a shortcut for saying /drives/<driveidofcaller>. When you're attempting to query someone elses drive you'd want /drives/DRIVEID, and so extending to your example you'd want something like:
GET /drives/DRIVEID/root/children