Creating nested folder in Sharepoint with Graph API fails - sharepoint

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.

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.

Microsoft Graph API - How to Delete a Folder with Children

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:

Microsoft Graph API calls for some functionalities

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

Azure Function GET request getting 401, but POST works

I have an Azure function that has a GET endpoint and a POST endpoint. In local testing they both work fine. When published to Azure, the POST endpoint works with the copied link from the Functions blade ('https://address/api/search?code=xxx'), but a GET request to the same endpoint returns 401 Unauthorized. Anyone else have this issue, or an idea on how to get this working? I've tried restarting the Function app, re-publishing, and stop-starting the app. All result in the same issue.
EDIT: Just for clarity, I'm using POST to do a detailed search with search terms included in the body, and GET to just retrieve all items.
EDIT2: I also see that the function.json visible in the Functions blade shows the following:
..."bindings": [
{
"type": "httpTrigger",
"route": "search",
"methods": [
"post"
],
"authLevel": "function",
"name": "req"
}
], ...
So it looks like my GET endpoint didn't even make it into the published function. This works locally with two functions sharing an endpoint, but differing request types, but doesn't seem to work in Azure. Any idea why?
I just figured it out. The functions in the Functions blade are listed by function name, not endpoint. I had built the functions in Visual Studio, so the compiler required unique names for the functions, which made sure I was naming them properly. I just missed the fact that each function name was separate, regardless of endpoint.

Deleting a feed using REST API

My requirement is to clear all activities on a notification feed.
Based on this stackoverflow question I understand that there is an undocumented REST API to delete a feed and the dashboard truncate feed functionality uses it.
I tried to replicate the call with the same parameters as dashboard:
DELETE /api/v1.0/feed/notification/f8fa1d12-594a-4b2b-ac58-23c912d1335a/?api_key=...&location=unspecified
Host: api.getstream.io
Authorization: notificationf8fa1d12-xxxx-xxxx-xxxx-23c912d1335a writetoken
stream-auth-type: simple
X-Stream-Client: stream-javascript-client-browser-unknown
Cache-Control: no-cache
Tried to use the same but am getting this error message:
{
"code": null,
"detail": "url signature missing or invalid",
"duration": "6ms",
"exception": "AuthenticationFailed",
"status_code": 403
}
Is this the right way to use this API?
I am using this from Java code and believe that the Java client doesn't have this functionality built in.
There's two ways to do this. You can do it manually from the explorer on the dashboard. Search for the feed, select an activity and press the truncate feed button. This is the easiest way to do this if manually doing this is sufficient.
It's also possible like you found to use the delete API endpoint to do it programmatically. This endpoint is not built in to most clients, including the Java client. The URL and HTTP verb that you used should indeed work.
From what I can tell from your headers and the response it seems like you are having an issue with supplying a correct signature. The easiest way to do this correctly is to use the built in methods in the library you're using to generate them. I'm not an expert in the Java library, but it seems like these methods are inside the StreamRepoUtils class.

Resources