Jason Good
Jason Good

Reputation: 11

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?

Upvotes: 1

Views: 1333

Answers (1)

Mario Varchmin
Mario Varchmin

Reputation: 3782

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

Upvotes: 0

Related Questions