How to read a file from a different user's onedrive - microsoft-graph-api

I have an excel file named "demo.xlsx" uploaded in the root folder of user A's onedrive. This file contains a table named "EmployeeTable". I want to login to my app as a different user - User 'B' and read data from this table.
I tried to login as user B and access the data using
https://graph.microsoft.com/v1.0/users/8e92a7895e495d6623/drive/root:/demo.xlsx:/workbook/tables('EmployeeTable')/rows
here 8e92a7895e495d6623 is user A's id. But I get the following error:
statusCode: 404,
code: "itemNotFound",
body."message":"Item does not exist"
I have tried to share the file with user B by logging in to onedrive as user A and using the 'share' option, but I still get the above error.
When I login to my app as user A, and use the same URI as mentioned above, I am able to retrieve the data correctly.
Can anyone guide me on how to read an excel file from a different user's onedrive folder? I want to be able to keep this excel file in a common area like a user's onedrive folder and have multiple users read from it.
Note: the app that I have built is based on https://github.com/microsoftgraph/angular-excelstarter-sample

If user A shares a file with user B and user B is currently logged in an app then call the endpoint
https://graph.microsoft.com/v1.0/me/drive/sharedWithMe
The response will contain all files shared with user B. Find a file you need. The response should contain remoteItem node with parentReference child node.
"#odata.type": "#microsoft.graph.driveItem",
"createdDateTime": "2021-01-05T09:51:23Z",
"id": "01BDYTYLL573DF4J3A2JG3WPFKMZLLBPQD",
...
"remoteItem": {
"id": "01BDYTYLL573DF4J3A2JG3WPFKMZLLBPQD",
"parentReference": {
"driveId": "b!RdjejBSERkqd6KyrlECzSlClLPMHvAxIle01z5BIuc0OlAR_j4bQQ5j8LT1dXapM",
"driveType": "business",
"id": "01BDYTYLN6Y2GOVW7725BZO354PWSELRRZ"
},
....
Use the driveId and remote item's id and call this endpoint to access data
https://graph.microsoft.com/v1.0/drives/{driveId}/items/{id}/workbook/tables('EmployeeTable')/rows

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

Problems with Microsoft Graph - DriveItem Add Permission

I'm trying to Share Files on a SharePoint Document Library that I have as a part of an Office 365 Developer Program instance.
My AD has a variety of users, some "native" users created in the AD manually and the rest are "guests" from different domains that my team and I work for.
I'm executing the following API request on the graph via code using NestJs (as per snippet). I've all the required Delegated Permissions in the Application Registration to do everything too.
REST View:
POST /drives/{drive-id}/items/{item-id}/invite
{
"requireSignIn": true,
"sendInvitation": false,
"roles": [
"sp.full control"
],
"recipients": [
{
"email": "xxx.xxx#xxx.com"
}
]
}
Code View:
//build list of all to add: PL, PLB, Main, Current User and whatever is added in DTO
const participantsToAdd = [project.projectLead]
.concat(project.projectLeadBackup)
.concat(project.participants.filter(p => newRoles.includes(p.participantRole.name)).map(p => p.user))
.map(u => ({
oid: u.microsoftId,
mail: u.mail,
}));
const permission = {
recipients: participantsToAdd.map(p => ({ email: p.mail })),
requireSignIn: true,
sendInvitation: false,
roles: ['sp.full control'],
};
// add the right permissions to the file
const result = await client.api(`/drives/${this.libraryId}/items/${fileId}/invite`).post(permission);
The above code is building up a list of "User" objects which contain an "oid" which I use later, and a "mail" object. I give these users "sp.full control" role on a file. Some are granted direct access and others are given links (grantedToIndentities) with write permissions.
This only seems to be happening when Guests on the active directory make the request; though it's only occurring for some guests. Two guest users in particular that I grant access to are fine, they get "Direct Access". Others go into the "link sharing" category. I don't see any differences in the users in AD anywhere.
I've tried looking through all admin sites (SharePoint, M365) and tweaked External Sharing permissions but the problem still persists.
When I invoke the action from a "native" user on AD to the Graph using the same request, it all works fine. All users (native and guests) are added with "direct access".
Can anyone share any thoughts? Hope I've given enough info.
Snippet from Graph response:
Image

How to perform a resumable Upload to a SharePoint Site (Not Root) Subfolder using MS Graph API

Documentation:
https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
I am able to get the Drive ID of the sub site using the sites/domain/drives api. Using this and the driveId/root/children I can confirm several folders on this drive.
If I use the API POST /drives/{driveId}/items/{itemId}/createUploadSession replacing the driveId with the drive ID and the itemId with the folder ID I get the following error
{
"error": {
"code": "nameAlreadyExists",
"message": "Cannot create an upload session on a folder",
"innerError": {
"request-id": "609e17d5-b3f8-455d-9d0f-4849872d8cfa",
"date": "2020-02-25T20:31:33"
}
}
}
If the folder ID is not the ItemID, what is an ItemID ? How do I specify the folder that I want to save a file to? Or a subfolder or folders? None of the otpions let me use anything other than ItemID
POST /drives/{driveId}/items/{itemId}/createUploadSession
POST /groups/{groupId}/drive/items/{itemId}/createUploadSession
POST /me/drive/items/{itemId}/createUploadSession
POST /sites/{siteId}/drive/items/{itemId}/createUploadSession
POST /users/{userId}/drive/items/{itemId}/createUploadSession
The request options in your question can only be used to upload a new version of an already existing document by using the id of the document. A folder does not have a document, that can be replace with a newer version. Therefore, it is not possible to create an upload session for a folder. You need to specify the name of the file as a relative path after the folder id in order to create an upload session for a new file in the folder:
https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/items/{folderId}:/{fileNameUploadFile}:/createUploadSession
You can also use the folder name instead of the folder id:
https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/root:/{folderName}/{fileNameUploadFile}:/createUploadSession

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

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.

Resources