7

Microsoft Graph Mailbag - Search DriveItems for Only Files - Microsoft 365 Devel...

 2 years ago
source link: https://devblogs.microsoft.com/microsoft365dev/microsoft-graph-mailbag-filter-search-driveitems-for-only-files/?WT_mc_id=DOP-MVP-4025064
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Microsoft Graph Mailbag – Search DriveItems for Only Files

Brian

February 10th, 2022

In today’s Microsoft Graph Mailbag post, we’ll use the Microsoft Search APIs on Microsoft Graph to return only files/documents.

Please be sure to follow this blog series using https://aka.ms/MSGraphMailbag or with RSS using https://devblogs.microsoft.com/microsoft365dev/tag/MSGraphMailbag.

Introduction

Recently, I was reviewing questions on Microsoft Q&A. I found an interesting question on How to differentiate between folders and files. The author asked how to use the Microsoft Search APIs on Microsoft Graph to search for only files/documents from OneDrive or SharePoint. By default, when you specify entityType of “driveItem,” this will return files, folders, pages, and news. The following is a sample query from Microsoft Graph Explorer to search for “driveItem” content.

POST https://graph.microsoft.com/v1.0/search/query

{
  "requests":[
    {
      "entityTypes":[
        "driveItem"
      ],
      "query":{
        "queryString":"contoso"
      }
    }
  ]
}

I ran the above request in one my lab tenants. The following screenshot shows the sample output. Notice the first result is a site page.

Solution

In order to filter for only files/documents, you can add either of the following (ContentType or ContentTypeId) to the “queryString” property within the “query” element of your request.

"query": {
  "queryString": "test document AND ContentType:Document"
}
"query": {
  "queryString": "test document AND ContentTypeId:0x0101*"
}

I updated my sample query and re-ran in my lab tenant. Notice that the sample response now has fewer search hits, and the first result is now a file.

Here is one of the final sample queries for filtering for only files/documents.

POST https://graph.microsoft.com/v1.0/search/query

{
  "requests":[
    {
      "entityTypes":[
        "driveItem"
      ],
      "query":{
        "queryString":"contoso AND ContentType:Document"
      }
    }
  ]
}

Conclusion

In this post we looked at the sample request to search for driveItems using the Microsoft Search APIs on Microsoft Graph. We then modified it to filter for only files/documents. You can use similar modifications to filter for other types of content as needed. Hopefully this will save you time and give you inspiration on how you can adapt the standard sample requests to retrieve the data you need with Microsoft Graph.

Today’s post was written by Brian T. Jackett, Principal PM with the Microsoft Graph Customer and Partner Experience (CPx) team. You can follow Brian on Twitter @BrianTJackett


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK