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
Related
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 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.
I am trying to find a way out using node js to fetch the list of files (xlsx and CSV files are of my interest) from User's Microsoft OneDrive. From the list, choose the file and then download it into the local system.
I can see Microsoft documentation about using OneDrive REST API here. But, since I am new at this, I am not really able to work this around. Any help will be appreciated.
I want to do something similar to what we can do with Google Drive where I could get a list of files along with their names and unique id and when the user chooses one file, by use of the unique id, I was able to download the required file. I am wondering if a similar thing can be done with OneDrive.
My progress so far:
I am able to ask users to provide their consent and get code in return to redirect Uri (localhost in my case).
The link that does this- https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={my_client_id}&scope=User.Read%20offline_access%20Files.ReadWrite.all%20Files.ReadWrite&response_type=code&response_mode=query&redirect_uri=https://localhost:3400
After getting the code, I exchanged it for refresh token.
To do this exchange, I used Postman for the POST request. The URL for that is https://login.microsoftonline.com/common/oauth2/v2.0/token?grant_type=authorization_code&client_id=your_app_client_id&code=use_the_code_returned_on_previous_step
Now that I have a refresh token, I want to do operations like listing the files on my OneDrive and select anyone to download.
I suggest looking into the Graph API as follows, and using the same auth_token received above. Instead of /me you can substitute a user ID as well.
Get information about the user's drive:
GET https://graph.microsoft.com/v1.0/me/drive
Get the root folder of the drive:
GET https://graph.microsoft.com/v1.0/me/drive/root/
Note: the documentation and the paths in the JSON results say "root:" but I haven't been able to get it work work with the colon.
Embedded in the result you should see:
"folder": {
"childCount": {whatever the count is}
},
To see the files that are in the folder:
GET https://graph.microsoft.com/v1.0/me/drive/root/children
Files will have an id and a name, both of which can be used to retrieve them:
"createdDateTime": "2020-07-05T18:08:37Z",
"eTag": "\"{29C06DFA-92AE-48D5-AF3D-149EF959030F},1\"",
"id": "01EC2X7VP2NXACTLUS2VEK6PIUT34VSAYP",
"lastModifiedDateTime": "2020-07-05T18:08:37Z",
"name": "wizard of wor.png",
To download a file by ID:
https://graph.microsoft.com/v1.0/me/drive/items/01EC2X7VP2NXACTLUS2VEK6PIUT34VSAYP/content
Or by path (in example below Wizard of Wor.png is the file name):
https://graph.microsoft.com/v1.0/me/drive/root/children/Wizard%20of%20Wor.png/content
Documentation sources:
https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
I try to use the MS graph API to work with sharepoint sites.
All works fine, except i can not find how to create a folder in root of a site (folders are documents library).
If i know the site id (SITEID) i can list folders in root of the site as drives
GET /v1.0/sites/SITEID/drives
I have ID for each item (it is like a drive ID). The i can list folders on drives (which are subfolders of top level folders on a site)
GET /v1.0/sites/SITEID/drives/DRIVEID/root/children
I can create folders in this place and subfolders
POST /v1.0/sites/SITEID/drives/DRIVEID/root/children
{
"name": "New Folder 2",
"folder": {}
}
But how to create a top level folder on a site? In fact, how to create new drive on a site?
I try to guess something like
POST /v1.0/sites/SITEID/drives
{
"name": "New Drive",
"folder": {}
}
But this doesn't work
Also, i tried to create a list https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/list_create
POST /v1.0/sites/SITEID/lists
{
"name": "Books",
"columns": [
{
"name": "Author",
"text": { }
},
{
"name": "PageCount",
"number": { }
}
],
"list": {
"template": "documentLibrary"
}
}
no success
I think you are wrong. Folders are not document libraries.
Drive is a document library. Document library is a list with template: document library, so drive is also a list.
Endpoint
GET /v1.0/sites/SITEID/drives
will return a list of lists with template: document library.
Endpoint
GET /v1.0/sites/SITEID/lists
will return all lists of all types.
To create new drive (a document library) you should create a new list with document library as template. Then you will have a new drive in a root of the site.
What problem do you have with creation of a new list?
If it is possible, how would I achieve the following URL rewrites using PouchDB Server?
At /index.html, display the HTML output of /index/_design/index/_show/index.html.
At /my_database/index.html, display /my_database/_design/my_database/_show/index.html.
My aim is to use PouchDB (and eventually CouchDB) as a stand-alone web server.
I am struggling to translate the rewrite documentation into working code.
Apache CouchDB uses an HTTP API and (consequently) can be used as a static web server--similar to Nginx or Apache HTTPD, but with the added bonus that you can also use MapReduce views, replication, and the other bits that make up Apache CouchDB.
Given just the core API you could store an entire static site as attachments on a single JSON document and serve each file from it's own URL. If that single document is a _design document, then you get the added value of the rewriter.
Here's an example faux JSON document that would do just that:
{
"_id": "_design/site",
"_attachments": {
"index.html": {
"content_type": "text/html",
"data": "..."
},
"images/logo.png": {
"content_type": "image/png",
"data": "..."
},
"rewrites": [
{
"from": "/",
"to": "index.html"
}
]
}
The actual value of the "data": "..." would be the base64 encoded version of the file. See the Creating Multiple Attachments example in the CouchDB Docs.
You can also use an admin UI for CouchDB such as Futon or Fauxton--available at http://localhost:5984/_utils--both of which offer file upload features. However, those systems will require that the JSON document exist first and will PUT the attachment into the database directly.
Once that's completed, you can then setup a virtual host entry in CouchDB (or Cloudant) which points to the _rewrite endpoint within that design document. Like so:
[vhosts]
example.com = /example-com/_design/site/_rewrite/
If you're not hosting on port 80, then you'll need to request the site at http://example.com:5984/.
Using a _show function (as in your example) is only necessary if you're wanting to transform the JSON into HTML (or different JSON, XML, CSV, etc). If you only want static hosting, then the option above works fabulously. ^_^
There are also great tools for creating these documents. couchapp.py and couchdb-push are the ones I use most often and both support the CouchApp filesystem mapping "spec".
Hope that helps!