Downloading Sharepoint File using Microsoft Graph API not showing content attribute - sharepoint

I'm trying to download a file from SharePoint online using Microsoft Graph API.
Graph API is not allowing me to use /Content.
This is the endpoint I'm using:
https://graph.microsoft.com/v1.0/sites/<host.sharepoint.com>/lists/<ListID>/items/<ItemID>
This is what get (Values Removed):
{
"#odata.context","#odata.etag","createdDateTime","eTag","id","lastModifiedDateTime","webUrl","createdBy"{"user": {"email","id","displayName"}}, "lastModifiedBy": {"user": {"email","id","displayName"}},
"parentReference": {"id"},
"contentType": {"id"},
"fields#odata.context",
"fields": {"#odata.etag","FileLeafRef","id","ContentType","Created", "AuthorLookupId","Modified","EditorLookupId","_CheckinComment", "LinkFilenameNoMenu","LinkFilename","DocIcon","FileSizeDisplay", "ItemChildCount","FolderChildCount","_ComplianceFlags","_ComplianceTag", "_ComplianceTagWrittenTime","_ComplianceTagUserId","_CommentCount", "_LikeCount","Edit","_UIVersionString","ParentVersionStringLookupId", "ParentLeafNameLookupId"
}
}
When i try to add /content i get:
{
"error": {
"code": "BadRequest",
"message": "Resource not found for the segment 'content'.",
"innerError": {
"request-id": "<ReqID>",
"date": "<date>"
}
}
}
How do i download this file using the API?

The endpoint https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id} returns ListItem resource but Content is a property of DriveItem resource which
Represents an item within a drive, like a document, photo, video, or
folder resource
that's the reason this error occurs.
Given the provided endpoint it could be modified to
GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/driveItem/content
to download a content

Related

MS Graph API - List Children in DriveItems is failing with 404(itemNotFound)

I'm using the API v1.0/sites/:id/drive/items/:parent/children to fetch the children informations of the drive item. I'm able get the response for some sites but for couple of sites I end up with the error 404(itemNotFound).
I passed siteId in place of :id & parentReferenceId in place of :parent.
{
"error": {
"code": "itemNotFound",
"message": "The resource could not be found.",
"innerError": {
"date": "2022-03-15T05:25:34",
"request-id": "2afc519e-95b0-45b0-bd91-3c716154cdcd",
"client-request-id": "2afc519e-95b0-45b0-bd91-3c716154cdcd"
}
}
}
What is the reason for this behavior?
Try this url pattern to get list of files and sub-folders
https://graph.microsoft.com/v1.0/sites/<SITE_ID>/drives/<DRIVE_ID>/root:<PATH_TO_NESTED_SUB_FOLDERS>:/children
Example:
https://graph.microsoft.com/v1.0/sites/9e9145345-7f9j-4695-8701-bae4c2d8a553/drives/b!HTaRnpV7lUaKdgmdflilU5eQE4tMetVNifXIJN28qvp68vQzkrpfTLPMl6eBQi_4/root:/InnerFolder/InnerFol2:/children

obtain item-id for microsoft graph api

I am trying to figure out how to obtain an "item-id" for a file in a sharepoint site for microsoft graph API.
Microsoft documentation: https://learn.microsoft.com/en-us/graph/api/driveitem-get?view=graph-rest-1.0&tabs=http
Here is what I tried for far:
Tried using https://developer.microsoft.com/en-us/graph/graph-explorer to guess the api link that will let me download the file I want.
I first tried to get the sharepointsite like so:
https://graph.microsoft.com/v1.0/sites/omidtechnfr.sharepoint.com:/sites/delploy
That worked:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites/$entity",
"createdDateTime": "2021-07-15T21:02:02.78Z",
"description": "delploy",
"id": "omidtechnfr.sharepoint.com,5e03397e-03b0-4d92-9658-f9163f0131b3,1d9f21da-4d74-45db-b5d4-e4a7f7f278a1",
"lastModifiedDateTime": "2021-07-17T02:16:39Z",
"name": "delploy",
"webUrl": "https://omidtechnfr.sharepoint.com/sites/delploy",
"displayName": "delploy",
"root": {},
"siteCollection": {
"hostname": "omidtechnfr.sharepoint.com"
}
}
So then I tried using what I ASSUME is the item-id for the SharePoint site delploy to try and get more information out of the site so I can try to find the item-id for the file I want to download:
https://graph.microsoft.com/v1.0/sites/5e03397e-03b0-4d92-9658-f9163f0131b3/drive/
And I got back some result but it doesn't so the item-id for me to do anything with it:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives/$entity",
"createdDateTime": "2021-07-11T05:16:50Z",
"description": "",
"id": "b!fjkDXrADkk2WWPkWPwExs9ohnx10TdtFtdTkp_fyeKF_K1_Z49FFSbzXVFRA5GKa",
"lastModifiedDateTime": "2021-07-16T18:32:35Z",
"name": "Documents",
"webUrl": "https://omidtechnfr.sharepoint.com/sites/delploy/Shared%20Documents",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"displayName": "System Account"
}
}
At this point I started to get desperate and a little angry. So I tried to just download the file from sharepoint and assume that the characters at the end of the download link was the item-id but that didn't work either:
download link:
https://omidtechnfr.sharepoint.com/sites/delploy/_layouts/15/download.aspx?UniqueId=25e2fd4f%2Deb32%2D43ac%2D9141%2Dfa47a610a27c
Used https://www.urldecoder.org/ to decode what I am assuming is the item-id for the file I want to download
25e2fd4f-eb32-43ac-9141-fa47a610a27c
Then tried to desperately tack this on to the Microsoft graph api to see what happens:
https://graph.microsoft.com/v1.0/sites/5e03397e-03b0-4d92-9658-f9163f0131b3/drive/items/25e2fd4f-eb32-43ac-9141-fa47a610a27c
and it failed:
{
"error": {
"code": "itemNotFound",
"message": "The resource could not be found.",
"innerError": {
"date": "2021-07-17T14:13:51",
"request-id": "985e2893-5343-47ac-a93c-6629a00c4eb3",
"client-request-id": "73a3b6d0-f1ce-bb51-b126-43f0957d15e3"
}
}
}
You could get the file by the path: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/{item-path}
For example: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/test.docx
If the file is in a folder, it would be like this: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/folder1/test.docx
I used the search site id endpoint GET /sites/{site-id}/drive/root/search(q='{search-text}')
Documentation here.

MS Graph API access to a workbook from a driveItem

I have written some code which successfully accesses an excel spreadsheet on a sharepoint site. I did this by finding the driveid in graph explorer then browsed to find the workbook item and worksheet.
Now I need to do the same thing but I can't use Graph Explorer to find the drive ID so I understood that I could generate the drive id using the method shown at https://www.techmikael.com/2018/07/using-microsoft-graph-to-get-pdf.html
( Side-note, my node.js version of the same code is at https://glitch.com/~ultra-sloop )
From this URL :
https://alphaindigo.sharepoint.com/:x:/r/_layouts/15/Doc.aspx?sourcedoc=%7B1F4C6E80-0A55-4717-8D8D-19382F8E3C69%7D&file=Buddies.xlsx&action=default&mobileredirect=true
I generate this one.
https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9hbHBoYWluZGlnby5zaGFyZXBvaW50LmNvbS86eDovci9fbGF5b3V0cy8xNS9Eb2MuYXNweD9zb3VyY2Vkb2M9JTdCMUY0QzZFODAtMEE1NS00NzE3LThEOEQtMTkzODJGOEUzQzY5JTdE/driveitem
When I run this in Graph Explorer as a test, it gives me the details of the driveItem, like this :
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#shares('u%21aHR0cHM6Ly9hbHBoYWluZGlnby5zaGFyZXBvaW50LmNvbS86eDovci9fbGF5b3V0cy8xNS9Eb2MuYXNweD9zb3VyY2Vkb2M9JTdCMUY0QzZFODAtMEE1NS00NzE3LThEOEQtMTkzODJGOEUzQzY5JTdE')/driveItem/$entity",
"#microsoft.graph.downloadUrl": "https://alphaindigo.sharepoint.com/_layouts/15/download.aspx?UniqueId=1f4c6e80-0a55-4717-8d8d-19382f8e3c69&Translate=false&tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvYWxwaGFpbmRpZ28uc2hhcmVwb2ludC5jb21AMGQ3NTY1NzUtMjgwOC00NmE1LWIyYmEtMDFlZTgzZmIwZTEzIiwiaXNzIjoiMDAwMDAwMDMtMDAwMC0wZmYxLWNlMDAtMDAwMDAwMDAwMDAwIiwibmJmIjoiMTU2Mjg5MzAyNSIsImV4cCI6IjE1NjI4OTY2MjUiLCJlbmRwb2ludHVybCI6InNzeDk3QjhPRk1mTXVTS1NyK2M5dFpITWtsdi9yR1BUbWdEL2hnSlhDVTQ9IiwiZW5kcG9pbnR1cmxMZW5ndGgiOiIxMjIiLCJpc2xvb3BiYWNrIjoiVHJ1ZSIsImNpZCI6Ik5qWTNPRFl4WTJNdE4yWXpNeTAwWTJFd0xUbGhaV1l0WkRnM05ERXpOV05sTW1GaSIsInZlciI6Imhhc2hlZHByb29mdG9rZW4iLCJzaXRlaWQiOiJORGM1TURWaE4yUXRObUl5TXkwME5EUTRMV0ZtWkdRdE5UVTJZamcwWlRNNFpUSmoiLCJhcHBfZGlzcGxheW5hbWUiOiJHcmFwaCBleHBsb3JlciIsInNpZ25pbl9zdGF0ZSI6IltcImttc2lcIl0iLCJhcHBpZCI6ImRlOGJjOGI1LWQ5ZjktNDhiMS1hOGFkLWI3NDhkYTcyNTA2NCIsInRpZCI6IjBkNzU2NTc1LTI4MDgtNDZhNS1iMmJhLTAxZWU4M2ZiMGUxMyIsInVwbiI6ImRvdWdAYWxwaGFpbmRpZ28ub25taWNyb3NvZnQuY29tIiwicHVpZCI6IjEwMDMyMDAwNDZDREY4QzQiLCJjYWNoZWtleSI6IjBoLmZ8bWVtYmVyc2hpcHwxMDAzMjAwMDQ2Y2RmOGM0QGxpdmUuY29tIiwic2NwIjoibXlmaWxlcy5yZWFkIGFsbGZpbGVzLnJlYWQgbXlmaWxlcy53cml0ZSBhbGxmaWxlcy53cml0ZSBhbGxzaXRlcy5yZWFkIGFsbHNpdGVzLndyaXRlIiwidHQiOiIyIiwidXNlUGVyc2lzdGVudENvb2tpZSI6bnVsbH0.L2t4YmRDQlpwS2dGMjIvdmxoVFA5ZXA0Qyt0b3VWWE9rb0h1UTJzcFVHRT0&ApiVersion=2.0",
"createdDateTime": "2019-05-05T04:52:56Z",
"eTag": "\"{1F4C6E80-0A55-4717-8D8D-19382F8E3C69},688\"",
"id": "01FQPFGAEANZGB6VIKC5DY3DIZHAXY4PDJ",
"lastModifiedDateTime": "2019-06-29T02:24:23Z",
"name": "Buddies.xlsx",
"webUrl": "https://alphaindigo.sharepoint.com/_layouts/15/Doc.aspx?sourcedoc=%7B1F4C6E80-0A55-4717-8D8D-19382F8E3C69%7D&file=Buddies.xlsx&action=default&mobileredirect=true",
"cTag": "\"c:{1F4C6E80-0A55-4717-8D8D-19382F8E3C69},690\"",
"size": 31938,
"createdBy": {
etc etc...
but when i attach /workbook/worksheet/file-name to the end
https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9hbHBoYWluZGlnby5zaGFyZXBvaW50LmNvbS86eDovci9fbGF5b3V0cy8xNS9Eb2MuYXNweD9zb3VyY2Vkb2M9JTdCMUY0QzZFODAtMEE1NS00NzE3LThEOEQtMTkzODJGOEUzQzY5JTdE/driveitem/workbook/worksheets('Sheet1')/range(address='A1:D4')
I get this error :
"error": {
"code": "InternalServerError",
"message": "Unable to find target address",
"innerError": {
"request-id": "5bea4b7f-91d9-4ba6-a26f-7964a4122a0a",
"date": "2019-07-12T00:36:16"
}
}
}
If I step back from there, dropping the worksheet name and range using this :
https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9hbHBoYWluZGlnby5zaGFyZXBvaW50LmNvbS86eDovci9fbGF5b3V0cy8xNS9Eb2MuYXNweD9zb3VyY2Vkb2M9JTdCMUY0QzZFODAtMEE1NS00NzE3LThEOEQtMTkzODJGOEUzQzY5JTdE/driveitem/workbook
I get the same error
A similar issue is already listed, answered at
Pulling Excel.xlsx workbook from Sharepoint site using Microsoft Graph API
but as I already have a driveItem rather than a listItem it does not answer my issue.
A workaournd:
json_response = GET("https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly9hbHBoYWluZGlnby5zaGFyZXBvaW50LmNvbS86eDovci9fbGF5b3V0cy8xNS9Eb2MuYXNweD9zb3VyY2Vkb2M9JTdCMUY0QzZFODAtMEE1NS00NzE3LThEOEQtMTkzODJGOEUzQzY5JTdE/driveitem")
drive_item = json_response["id"]
drive = json_response["parentReference"]["driveId"]
print(GET("https://graph.microsoft.com/v1.0/me/drives/{drive}/items/{drive_item}/workbook"))

Adding User to Group using Graph API B2C

I am using Azure AD B2C to create users.
After a user is created I would like to add them to a Group
Using Postman I have made the following request (with access token in the header). Note: I have managed to successfully create a group using the Api.
POST
https://graph.windows.net/{azure-tenant}/groups/{group-objectId/members/$ref?api-version=1.6
With Body:
{
"#odata.id": "https://graph.windows.net/{azure-tenant}/directoryObjects/{user-objectId}"
}
Which is what the documentation specifies. No matter the body that is sent I get the following error
{
"odata.error": {
"code": "Request_BadRequest",
"message": {
"lang": "en",
"value": "The request URI is not valid. Since the segment 'members' refers to a collection, this must be the last segment in the request URI. All intermediate segments must refer to a single resource."
},
"requestId": "48cf65f3-20ba-411e-8121-f7ea54252f3a",
"date": "2019-05-27T06:09:25"
}
}
I tried removing the /$ref
POST
https://graph.windows.net/{azure-tenant}/groups/{group-objectId/members?api-version=1.6
Body:
{
"#odata.id": "https://graph.windows.net/{azure-tenant}/directoryObjects/{user-objectId}"
}
as well as
{
"#odata.id": "https://graph.windows.net/{azure-tenant}/users/{user-objectId}"
}
Now the error that gets returned now is:
{
"odata.error": {
"code": "Request_BadRequest",
"message": {
"lang": "en",
"value": "Unsupported resource type 'DirectoryObject' for operation 'Create'."
},
"requestId": "2c36cc6d-383c-44f8-8609-2ac3e3efc862",
"date": "2019-05-27T06:15:26"
}
}
The documentation that I have been using https://learn.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=javascript
You are using graph.windows.net endpoint, so you should refer to azure ad graph api.
POST https://graph.windows.net/myorganization/groups/b4bda672-1fba-4711-8fb1-5383c40b2c14/$links/members?api-version=1.6
{
"url": "https://graph.windows.net/myorganization/directoryObjects/3eb6055a-baeb-44d4-a1ea-2fee86d8891b"
}
The document you referred to is microsoft graph api. The endpoint should be https://graph.microsoft.com.
You can use Microsoft Graph explorer to call these apis.

Microsoft Graph(German cloud) - Get root site (sites/root)

When making a call to the Microsoft German Graph /sites/root endpoint, it returns 'Resource Not Found', although that in global Graph the same endpoint works perfectly ok
Here is an example:
https://graph.microsoft.de/v1.0/sites/root
It returns:
{
"error": {
"code": "BadRequest",
"message": "Resource not found for the segment 'sites'.",
"innerError": {
"request-id": "aff4a613-54ed-476d-976f-d4ac7a8c7ee2",
"date": "2018-04-26T07:59:17"
}
}
}
Found an answer, for now, this service is deployed only in the "beta" API
So I used https://graph.microsoft.de/beta/sites/root and it works

Resources