Microsoft Graph API - How to Delete a Folder with Children - azure

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:

Related

How to get file comments using Sharepoint rest api

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.

SharePoint REST API Setting CheckoutUser using ValidateUpdateListItem()

I am trying to update the CheckoutUser for a file in SharePoint because when the file is checked out in my code, it is being checked out by a SharePoint admin account and I want the CheckoutUser to show the person who actually checked it out. Whenever I make a request to _api/web/GetList(#path)/items(id)/ValidateUpdateListItem()?#path='/url' with the user info, I get a 200 response back and no errors, but for some reason the CheckoutUser value is cleared instead up updating to be the user that I want to set. This is what the request body looks like:
{
"formValues": [
{
"FieldName": "CheckoutUser",
"FieldValue": "[{'Key': 'i:0#.w|user.name'}]"
}
],
"bNewDocumentUpdate": true
}
I have tested out requests to both SharePoint 2016 and SharePoint Online using Postman and have not been able to get the CheckoutUser to update in either case.

Creating nested folder in Sharepoint with Graph API fails

I can create a nested folder /folder1/folder2 in (personal) OneDrive with the following Graph request (using the beta API):
POST drives/{id}/root:/folder1:/children
{
"name": "folder2",
"folder": {},
"#microsoft.graph.conflictBehavior": "fail"
}
Here, folder1 didn't exist beforehand; the one API call creates both it and folder1/folder2.
However, the same code fails for SharePoint and OneDrive for Business with a 404 error. Is this type of request supported? I'd rather not have to make a separate API call for every level of nesting.
I'm asking this because I'm not sure whether my site has policies that cause the request to fail, or because the API actually doesn't support it.
This works for me. Remove name parameter from the body and use PATCH instead of POST. Specify the path in the URL.
PATCH drives/{id}/root:/folder1/folder2
{
"folder": {},
"#microsoft.graph.conflictBehavior": "fail"
}
Tested using v1.0 and beta for OneDrive for Business.

Accessing Files in a SharePoint Site via Graph: Bad Request

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

Does ms graph API support outlook on office 365 on Germany endpoint?

I found that graph API in Germany endpoint seems not to support
GET /users/<id>/mailFolders/delta
it returns
**400**
{
"error": {
"code": "ErrorInvalidIdMalformed",
"message": "Id is malformed.",
"innerError": {
"request-id": "cf713386-f050-40b4-b987-43cc2384eade",
"date": "2017-12-12T07:13:10"
}
}
}
However it works fine at
GET /users/<id>/mailFolders
Thanks for help
I had been in the same trouble, but I resolved.
Your request to:
GET /users/<id>/mailFolders/delta
is not correct, I think.
I guess that this mail folder delta was made by you, doesn't it?
To specify a mail folder, you need to use folder id, not display name.
Though you might already get the folder id, you can get the folder id by sending a request
GET /users/<id>/mailFolders
Note: Actually, we can get folder_ids via powershell:
Get-MailboxFolderStatistics
cmdlet on Exchange. However, these folder_id are malformed in ms-graph request. So you need to use the id which you can get as described above.

Resources