In order to create a new file in a drive (a simple text file with some content), I try to call an API with MS Graph.
PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
The drive where I want to put the file is name "myFolder" and has this driveId :
b!e5bDF5eRbU2Y7P3gHeS-0F1abPhpWXdOvSUViyWpqX876IbeJvIPS5-tf--QTQiz
This drive is in the root site.
Here :
{site-id} : "root"
{parent-id} : is the id of the drive which will be the parent
{filename} : text.txt
If I had to create this file in a subsite it would
{site-id}:/sites/{subsite-id}/drive/items/{parent-id}:/{filename}:/content
So theoretically my URL should be the following :
https://graph.microsoft.com/v1.0/sites/root/drives/b!e5bDF5eRbU2Y7P3gHeS-0F1abPhpWXdOvSUViyWpqX876IbeJvIPS5-tf--QTQiz:/text.txt:/content
Here :
{site-id} : "root"
{parent-id} : is the id of the drive which will be the parent
{filename} : text.txt
I also define the request header Content-Type as text/plain
My issue is that I retrieve an error message :
{
"error": {
"code": "BadRequest",
"message": "Resource not found for the segment 'content'.",
"innerError": {
"date": "2020-06-26T14:19:11",
"request-id": "812b7ee0-3ecb-4d41-a8c5-59419b086f51"
}
}
}
This is expected error since for addressing a file within a drive (which is document library in SharePoint) a folder container needs to be provided:
So, instead of query
https://graph.microsoft.com/v1.0/sites/root/drives/{drive-id}:/{file-name}:/content
it should be
https://graph.microsoft.com/v1.0/sites/root/drives/{drive-id}/root:/{folder-path}/{file-name}:/content
where folder-path corresponds to Test folder in your case
References
Addressing resources in a drive on OneDrive
You can directly create file in subsite library using below endpoints:
https://graph.microsoft.com/v1.0/sites/{sub-siteid}/drives/{driveid}/root:/FolderA/FileB.txt:/content
Reference doc:
https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http#example-upload-a-new-file
And if you want to get subsite id, you can refer to :
Access a site by server-relative URL
Demo request:
Related
I'm using the following api to successfully get file data:
https://acme.sharepoint.com/sites/my-site/_api/Web/Lists(guid'xxx')/files('yyy')
This is a docx file on which I've posted comments using the web console.
How can I fetch these comments using the rest api? I tried appending /comments to the url, but I'm getting the following 404 error:
{
"error": {
"code": "-1, Microsoft.SharePoint.Client.ResourceNotFoundException",
"message": {
"lang": "en-US",
"value": "Cannot find resource for the request Comments."
}
}
}
The Comments() endpoint currently exists only under the Items() endpoint and not under the Files() endpoint.
Basically, you can access the Comments() functionality only under the below endpoint:
GET https://{site_url}/_api/web/lists/GetByTitle({list_title})/items({item_id})/Comments
You can easily test the above in a PowerAutomate scenario with a Send Http Request to SharePoint actions.
In the below example I attempt to target the file in the document library:
On the other hand, if I attempt to target the file based on the List Item Id that it got in the document library I will get the below response:
As you can see from the above, I am also able to target a specific comment that I left.
Please take note of the below
The Comments() endpoint is not available for MS resources, meaning docx, excels and such files. It is only available for non-MS resource files like pdfs, txts and so on. I am not sure why this rule is in effect but, my best guess would be because there is a "commenting" functionality provided within a Word Document, for example.
You could find a bit more info about the above here.
I could use some help deleting a folder using the Microsoft Graph API. The api call below works, but only when the folder is empty and has no children.
DELETE https://graph.microsoft.com/v1.0/drives/{{driveId}}/items/{{driveItemId}}
The folders are created and deleted using the same client application and permissions
It's a folder in a Sharepoint document library if that matters at all
I tried adding an If-Match header with the current eTag of the folder, but that didn't make any difference.
Here's the response I'm getting:
409 Conflict
{
"error": {
"code": "resourceModified",
"message": "The resource has changed since the caller last read it; usually an eTag mismatch",
"innerError": {
"date": "2022-10-17T21:40:30",
"request-id": "b3d35695-...",
"client-request-id": "b3d35695-..."
}
}
}
Is there a way to force the delete, or do I have to clear it out before I can delete it? If I have to clear it out, what's the easiest way to do that?
I tried to reproduce the same in my environment and got below results:
I have one Azure AD application and granted API permissions like below:
To call Microsoft Graph, I acquired access token using client credentials flow via Postman as below:
I created folder in my SharePoint document library using above token with below query:
POST https://graph.microsoft.com/v1.0/drives/<driveID>/items/
{
"name": "Sri Folder",
"folder": { },
}
Response:
When I checked in SharePoint Online, folder created successfully in document library like below:
Now I added children to the created folder by uploading one file to it with below query:
PUT https://graph.microsoft.com/v1.0/drives/<driveID>/items/<driveItemId>:/test.txt:/content
Response:
When I checked the same in SharePoint Online, file is uploaded to the folder successfully like below:
Now, I used the same query as you to delete the folder with children by adding If-match header and got same error like below:
DELETE https://graph.microsoft.com/v1.0/drives/<driveId>/items/<driveItemId>
If-match : etag_value
Response
To resolve the error, you need to replace If-match header with eTag.
I tried the same query by replacing If-match header with eTag, it returned Status: 204 No Content like below:
Response:
When I refreshed the page in SharePoint Online, got this error as folder got deleted along with its children:
I checked opening document library and cannot find the folder as it got deleted successfully:
I am using Sharepoint Office 365, but am new to Microsoft Graph APIs. I have 2 functionalities, namely create a folder at a specific location and upload a file at that location.
Suppose, the site url is:
'https://abcdef.sharepoint.com/sites/folder1/folder2'
So, first I have to create directory /folder1/folder2 inside sites, and then, need to upload a file (maybe text.txt) inside folder2.
I just need the subsequent Graph API calls which are needed to achieve the required functionality.
Note: I don't need any code structure, just the API calls required (in sequence).
You can follow this steps:
Create folder1.
POST https://graph.microsoft.com/v1.0/sites/{site id}/drive/root/children
{
"name": "folder1",
"folder": {},
"#microsoft.graph.conflictBehavior": "rename"
}
Create a subfolder folder2 in folder1.
POST https://graph.microsoft.com/v1.0/sites/{site id}/drive/items/{folder1 item id}/children
{
"name": "folder2",
"folder": {},
"#microsoft.graph.conflictBehavior": "rename"
}
Upload a file to folder2.
PUT https://graph.microsoft.com/v1.0/sites/{site id}/drive/items/{folder2 item id}:/file.txt:/content
Content-Type: text/plain
The contents of the file goes here.
Refer to: here
The documentation for using the MS Graph 1.0 API for accessing SharePoint files from libraries seems clear enough, if a bit indirect. My understanding is that I should be able to access the top level item of a library (and then its children via /children) by the following url scheme:
https://graph.microsoft.com/v1.0/sites/<my-tenant>.sharepoint.com:/sites/my-test-site:/drive/root
But I am only getting back an error telling me the Url is invalid:
{
"error": {
"code": "BadRequest",
"message": "Url specified is invalid.",
"innerError": {
"request-id": "08bb72aa-f3be-4df0-b253-dacc4a8fe390",
"date": "2019-07-08T16:38:07"
}
}
}
I've tried a few other url formats as well, such as specifying the drive specifically by Id /drives/<driveId>/root but had the same luck. I'm sure I am misunderstanding something. I'm using the "Path" format (:/sites/path-to-site:/ in the API because it is more natural than going and fetching an Id for everything I need to query.
You need provide global Id of the site you want to access (global Id is <hostName>,<siteCollectionId>,<siteId>).To get the global id, in you test, we can use this.
https://graph.microsoft.com/v1.0/sites/<my-tenant>.sharepoint.com:/sites/my-test-site:/
And below API gives us a list of files on a specified site's default drive:
https://graph.microsoft.com/v1.0/sites/<hostName>,<siteCollectionId>,<siteId>/drive/root/children
If you want to access files on a specific list, all you need is the id of the list:
https://graph.microsoft.com/v1.0/sites/<hostName>,<siteCollectionId>,<siteId>/lists/<listId>/drive/root/children
I am trying to create file handler add in that uses provider hosted application to open custom file.
I have hosted app in heroku and until now i get request body in my application as follows when i open file from sharepoint.
{
resourceid: "https://{tenant}.sharepoint.com",
culturename: "en-US",
fileget: "https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}/contents?access_token={token}",
fileput: "https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}/contents?access_token={token}",
fileid: "{fileid}",
client: "SharePoint"
}
But when i request to get file content using fileget url. It throws 500 error with message
The URL is invalid for the current user or application.
What could be the way to access file content.
The URL seem to be correct.
What you can do is to see the file's URL properties by requesting CheckFileInfo.
Example:
GET https://{tenant}.sharepoint.com/_vti_bin/wopi.ashx/files/{fileid}?access_token={token}
And have a look at DownloadUrl and FileUrl. If any of the properties is set, use them to download the file.