Anonymously uploading to a publicly shared folder on OneDrive - microsoft-graph-api

What is the REST API call to anonymously upload to a publicly shared folder on OneDrive?
I have tried sharing a folder both thru the OneDrive Web UI creating a link attributed with: "Anyone with this link can edit this item", and using the REST API:
POST https://graph.microsoft.com/v1.0/drives/{driveId}/items/{sharedFolderId}/createLink
Content-type: application/json
{
"type": "edit",
"scope": "anonymous"
}
In both cases, I can read from the shared folder without logging on by
GET https://api.onedrive.com/v1.0/shares/{shareId}/items/{sharedFolderId}
I can also read the permission itself using
GET https://api.onedrive.com/v1.0/shares/{shareId}/items/{sharedFolderId}/permissions
=>
{
"#odata.context":"https://api.onedrive.com/v1.0/$metadata#shares('{shareId}')/items('{sharedFolderId')/permissions",
"value":
[
{
"id":"{permissionId}",
"link":
{
"application":
{
"displayName":"{my own app}",
"id":"{short app id}"
},
"type":"edit",
"webUrl":"https://1drv.ms/u/{shareId}"
},
"roles":["write"],
"shareId":"{shareId}",
"expirationDateTime":"0001-01-01T00:00:00Z",
"hasPassword":false
}
]
}
However trying to upload a file or create a subfolder, i.e.
PUT https://api.onedrive.com/v1.0/shares/{shareId}/driveItem:/{filename}:/content
Content-type: text/plain
some text goes here
or
POST https://api.onedrive.com/v1.0/shares/{shareId}/items/{sharedFolderId}/children
Content-type: application/json
{
"name": "TestFolder",
"folder": { }
}
both fail as unauthorized calls - but isn't the whole point of an "edit" link with "anonymous" scope that "anyone with this link can edit this item"?
I have tried various combinations of https://graph.microsoft.com/v1.0 instead of https://api.onedrive.com/v1.0 and /drives/{driveId} instead of /shares/{shareId} as well as /shares/{shareToken}, where shareToken is the "u!"-encoding of the webUrl from the link in the permission.
So far without being able to figure out the right REST API call. I hope someone is able to help :-)
You can download my TestOneDrive Visual Studio test project to reproduce the issues. It also contains initialization code to create and share the folder.

Due to no guys from Product Group following this and no official docs announced this, so I suggest you to submit an feature request first on UserVocie or vote up an existing one close to your issue.

Related

How do I get the mimetype and file ID using the Microsoft Graph API for OneDrive?

We are integrating our product with MS OneDrive. Our users will connect their MS accounts and choose a folder that they want us to pull files from. Our system only supports certain file types so we have to get the mimetype information AND the file ID after making a request. But we can't find a request that brings back both pieces of information so we can determine if we should pull a file or not.
This is close, but it doesn't give us the mimetype information.
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_recent?view=odsp-graph-online
Anyone have any ideas?
The get method lets you retrieve all kinds of information about a file: https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
It returns a driveItem object, that has a file property. And that file property has a mimeType property. See here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/driveitem?view=odsp-graph-online and here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/file?view=odsp-graph-online.
Unfortunately that means, that you will need more than one request to get the information you want.
If filtering by filename suffix is good enough for you, you can query OneDrive like this:
POST /search/query
Content-Type: application/json
{
"requests": [
{
"entityTypes": [
"driveItem"
],
"query": {
"queryString": "filetype:docx OR filetype:doc"
}
}
]
}
Documentation: https://learn.microsoft.com/en-us/graph/search-concept-files#example-5-use-filters-in-search-queries

How to copy an excel file using microsoft graph API?

I am trying to copy an excel file in the same folder in my sharepoint account using following API - POST /drives/{driveId}/items/{itemId}/copy but it gives message 'Operation not supported'.So how to do this?
As discussed in the comments you can try the below scenario.
I have a folder in my drive and it has an excel file. I am trying to copy that file into same folder by using the below call.
https://graph.microsoft.com/v1.0/sites/{siteid}/drives/{driveid}/Items/{file id which you want to copy}/copy
Request Body:
{
"parentReference": {
"driveId": "{Same driveid of above call}",
"id": "{Folderid}"
},
"name": "contosoplan(copy).xlsx"
}
Give a try with this. And as you said the apiversion=2.1 should not me used in some scenarios according to this Github Thread as it doesn't have feature parity with default version.

Create a New Folder using Microsoft Graph API

I am trying to create a new folder under the root of my onedrive by making a call to the following end point :
Request:
POST graph.microsoft.com/v1.0/me/drive/items/rootId/children
Content-Type: application/json
{
"name": "New Folder",
"folder": { },
"#microsoft.graph.conflictBehavior": "rename"
}
Expected Response:
HTTP/1.1 201 Created
Content-Type: application/json
{
"createdBy": {
"user": {
...
Obtained Response:
200 OK
When I make a call to this endpoint using Postman, I get a list of children under the root folder and the folder is not created.
Is the any way to resolve this?
Is there anything that i am doing wrong?
Kindly let me know.
use HTTPS request for all Microsoft Graph APIs
POST https://graph.microsoft.com/v1.0/me/drive/items/<rootId>/children
Can you provide a link to the docs page with that endpoint?
You can try make request to drive/root/children
below is a c# code for that:
var folder = new DriveItem { Name = "New Folder", Folder = new Folder() };
await client.Drive.Root.Children.Request().AddAsync(folder);
I used it a bit differently for something else, but should work analogically for you.
As other workaround, you can try to get the id of your drive id and drive root item id and then try to use the the endpoint without "me/" and "drives/{driveId}/items/{rootItemId}" instead.
PS. Have you inspected your oneDrive to obesrve the results of your calls? What do they produce?

Download shared file with Microsoft Graph API

I have to get data from a excel document which is shared with me. I have to get the cell colors of a lot of cells which takes very long with the graph api since you can only get them cell by cell. So i would like to download it and process it offline.
However since the file is not on my drive I'm not getting the #microsoft.graph.downloadUrl field with the file details request, and I would need to go with the Download File endpoint, which unfortunately returns 404. Is there any suggestion how can this be done?
According to your questions, you want to download a file which is someone shared with you. If I have misunderstand your, please let me know.
I have a preliminary solution like this:
First, we can use the List items shared with the signed-in user API to get the file which is shared with you. The part of the response like this:
{
"id": "1312abc",
"remoteItem": {
"id": "{itemid}",
"name": "March Proposal.docx",
"file": { },
"size": 19121,
"parentReference": {
"driveId": "{driveid}",
"id": "1991210caf!104"
}
}
}
}
From the response, we can get the drive's id and the item's id of the shared file.
Second, we can use the drive's id and the item's id to download the file. The API like this:
GET /drives/{drive-id}/items/{item-id}/content
For more detail, we can refer to the content of Download the contents of a DriveItem

Using createLink to share with specific users

I am trying to create a link to share a document with createLink from Microsoft Graph for specific users without using an invite but it is creating a link with Anyone with the link can edit this document permission.
I'm calling this endpoint:
POST /me/drive/items/{itemId}/createLink
With this request body:
{
"type": "edit",
"scope": "anonymous"
}
Am I missing something?
You can't use createLink for sharing with a specific person, you need to use the invite endpoint for that.
The call you're making is responding exactly how you've asked it to and generating a link (createLink) that anyone (anonymous) can access.
If you don't want to send a physical invitation, you can tell OneDrive this by setting the sendInvitation property to false:
POST /me/drive/items/{item-id}/invite
Content-type: application/json
{
"requireSignIn": true,
"sendInvitation": false,
"roles": [ "write", "read"],
"recipients": [
{
"email": "someone#contoso.org"
}
]
}
Please refer this link https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_invite
You can share the file with specific user by adding user's in
"recipients":[{"#odata.type":"microsoft.graph.driveRecipient"}]
parameter. You can set sendInvitation parameter like "sendInvitation":false to avoid sending the invitation mail.

Resources