MS Graph API access to a workbook from a driveItem - node.js

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.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvYWxwaGFpbmRpZ28uc2hhcmVwb2ludC5jb21AMGQ3NTY1NzUtMjgwOC00NmE1LWIyYmEtMDFlZTgzZmIwZTEzIiwiaXNzIjoiMDAwMDAwMDMtMDAwMC0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
"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"))

Related

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.

Microsoft Graph Search 400 error when searching

I'm getting a 400 error when trying to do a search both through the Graph Explorer and through my own code. Both worked a few weeks ago but have stopped working recently. Another developer has been able to replicate it in their Tenancy, but Microsoft will not provide me support as we are not a Premier Customer.
If I run this query:
https://graph.microsoft.com/v1.0/drives{drive_id}/root/
it succeeds and returns information about the drive.
Following the documentation at https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http a search can be used via
https://graph.microsoft.com/v1.0/drives/{drive-id}/root/search(q='foobar')
But this returns a 400 error with "One of the provided arguments is not acceptable."
However, if I search for items in our entire Tenancy via the group search it succeeds so it doesn't appear to be a permissions error, but is not a viable solution as I need to search a particular drive.
please confirm whether you are still facing the issue. I was not able to reproduce the same from my side.
I ran this query:
https://graph.microsoft.com/v1.0/me/drives/drive-id/root/Search(q='Practices Checker')
And was able to get the desired result as below:
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
"value": [
{
"#odata.type": "#microsoft.graph.driveItem",
"createdDateTime": "2020-12-09T11:55:47Z",
"id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"lastModifiedDateTime": "2020-11-03T06:51:27Z",
"name": "Practices Checker",
"webUrl": "https://microsoftapc-my.sharepoint.com/personal/ribera_xxxxxxxx_com/Documents/Documents/Practices%20Checker",
"size": 0,
"createdBy": {
"user": {
"email": "ribera#xxxxxxxxxx.com",
"displayName": "Rinki Bera"
}
},
"lastModifiedBy": {
"user": {
"email": "ribera#xxxxxxxxxx.com",
"displayName": "Rinki Bera"
}
},
...
]
}
Thank you.

Downloading Sharepoint File using Microsoft Graph API not showing content attribute

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

Get all root sites listed in share point Admin center using graph api beta

I need all root sites listed in share point Admin center shown in image
For that i am using Graph api like follows,
var settings = {
"async": true,
"crossDomain": true,
"url": "https://graph.microsoft.com/beta/sharepoint/sites",
"method": "GET",
"headers": {
"authorization": "Bearer token",
"cache-control": "no-cache",
"postman-token": "3116b007-e574-5ad4-aedd-3b35fbf76b61"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
But it gives output as follows,
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#sharePoint/sites",
"value": [
{
"createdDateTime": "2017-02-18T13:03:01.263Z",
"description": "",
"id": "2422c3a2-3c51-40f1-8483-5454aead43c4,412bb897-c754-460a-962d-db22893a1649",
"lastModifiedDateTime": "2017-04-24T02:16:43Z",
"name": "",
"webUrl": "https://mps330124.sharepoint.com",
"root": {},
"siteCollection": {
"hostname": "mps330124.sharepoint.com"
},
"siteCollectionId": "2422c3a2-3c51-40f1-8483-5454aead43c4",
"siteId": "412bb897-c754-460a-962d-db22893a1649"
}
]
}
How can i achieve it Using graph api beta
Using the asterisk on the site search endpoint seems to work
https://graph.microsoft.com/v1.0/sites?search=*
returns
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites",
"value": [
{
"createdDateTime": "2017-09-15T01:11:50Z",
"description": "Let's capture our thoughts in this subsite blog.",
"id": "xyz.sharepoint.com,5a58bb09-1fba-41c1-8125-69da264370a0,5e9767b8-95bc-4bd1-aeb0-defabcdefabc",
"lastModifiedDateTime": "0001-01-01T08:00:00Z",
"name": "internalblogs",
"webUrl": "https://xyz.sharepoint.com/internalblogs",
"displayName": "Internal blog"
},
{
...
}]
}
There isn't a way to retrieve a full list of site collections via Microsoft Graph. That functionality is "admin only" in the SharePoint API and has not been made available via Microsoft Graph.
We're working on some changes to the SharePoint Sites API for Microsoft Graph that will be rolled out over the next few weeks to make it possible to search for a list of sites / site collections, but we still don't have plans to make available the ability to enumerate all sites / site collections via Microsoft Graph.
Watch the Change Log around the Build 2017 conference (May 10-12) to find our all the details.
There now seems to be a way to get a list of the top level site collections via the following request:
GET https://graph.microsoft.com/beta/sites?select=siteCollection,webUrl&filter=siteCollection/root%20ne%20null
The following page documents this (update September 19th): https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/site_list
My own test though didn't bear any fruit, because the API returns a Cannot enumerate sites error. An alternative approach would be to use the search query parameter (docs updated September 20th):
GET https://graph.microsoft.com/beta/sites?search=contoso&select=siteCollection,WebUrl&filter=siteCollection/root ne null
But this only provides the siteCollection for the root :/ with a list of all sites and their webUrl of the other siteCollection's (like the default ones /sites & /teams) but not the actual url/hostname of the siteCollection they belong to. Though that could be extracted from the webUrl path component.
What you are getting is not an error.
If you follow the siteCollectionId with the URL https://graph.microsoft.com/beta/sharepoint/sites/2422c3a2-3c51-40f1-8483-5454aead43c4/sites you get a list of the sites in that site collection.

How to get lists from sharepoint site using graph api beta

I'm following documentation from here https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/sharepoint
I want to enumerate lists from default site.
get siteId using /sharePoint/site end point(works fine)
Request lists using sharePoint/sites/{{siteId}}/lists doesn't work returns 404 with following message
{
"error": {
"code": "itemNotFound",
"message": "Requested site could not be found",
"innerError": {
"request-id": "34b1b51a-59ba-4c56-991a-319ffbe33005",
"date": "2017-04-07T10:46:40"
}
}
}
any pointers
in sharePoint/sites/{{siteId}}/lists the part of {siteId} is actually 3 part, the domain, root site id, subsite id, for for example if i have a domain contoso.sharepoint.com, with a site named root site with id 123 (it should be a guid) and another subsite under that named Klingon Site with id of 145 so my final {siteId} is contoso.sharepoint.com,123,145
should be like contoso.sharepoint.com,fffdf23f-1234-1234-a744-17e2e196e777,1481eca1-4567-4567-8dbc-60c666ab3b8c
I'm assuming you have confirmed there is a list within this site. You'll get a 404 if you attempt to fetch a list collection from a site without one.
More likely is you're using the wrong ID. The documentation is a bit ambiguous about which property it means by {site-id}.
When you fetch your site, there will be both a siteId and an id property. You want the id property. Using siteId will result in a 404.
{
"createdDateTime": "",
"description": "",
"id": "**** Use this one ****",
"lastModifiedDateTime": "",
"name": "",
"webUrl": "",
"root": {},
"siteCollection": {
"hostname": ""
},
"siteCollectionId": "",
"siteId": "**** Not this one ****"
}

Resources