How to copy an excel file using microsoft graph API? - 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.

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

Microsoft Graph API create folder with custom metadata column

As part of creating a Team in Microsoft teams using the Graph API, I'm trying to create a folder in Microsoft Graph with a custom column - just a simple yes/no.
This is what I have so far:
var newFolder = new DriveItem();
newFolder.Name = folderName;
newFolder.Folder = new Folder();
newFolder.AdditionalData = new Dictionary<string, object>();
newFolder.AdditionalData.Add("#microsoft.graph.conflictBehavior", "rename");
var newSubFolder = _graphClient.Groups[team.RemoteId].Drive.Items[parentFolder.RemoteId].Children
.Request().AddAsync(newFolder).Result;
However I need to add a custom column to this folder at creation time (or just after, if it needs to be in an update call). I was hoping I could do this easily through the Graph API but I can't seem to find any way to do this. Does anyone know how? (I have no idea how to access the underlying SharePoint API, by the way, so even if I knew how to do it with SharePoint that wouldn't help me much at the moment.)
Try the following steps:
(1) Create the folder:
POST /drives/{drive-id}/items/root/children
{ "name": "{test}", "folder": { }, "#microsoft.graph.conflictBehavior": "fail" }
(2) Get the created folder's item id and update the folder's content type, metadata:
PATCH /sites/{site-id}/lists/{library-name}/items/{item-id} { "contentType": { "id": "{content-type-id}" }, "fields": { "fieldname1": "{value}", "fieldname": "{value}" } }

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

Anonymously uploading to a publicly shared folder on OneDrive

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.

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

Resources