{"id":9457,"date":"2022-02-10T11:09:08","date_gmt":"2022-02-10T19:09:08","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/?p=9457"},"modified":"2022-10-12T16:12:05","modified_gmt":"2022-10-12T23:12:05","slug":"microsoft-graph-mailbag-filter-search-driveitems-for-only-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/microsoft-graph-mailbag-filter-search-driveitems-for-only-files\/","title":{"rendered":"Microsoft Graph Mailbag &#8211; Search DriveItems for Only Files"},"content":{"rendered":"<p>In today\u2019s Microsoft Graph Mailbag post, we\u2019ll use the Microsoft Search APIs on Microsoft Graph to return only files\/documents.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0.jpg\"><img decoding=\"async\" class=\"aligncenter size-large wp-image-9494\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0-1024x335.jpg\" alt=\"Microsoft Graph logo with search magnifying glass and document icons\" width=\"640\" height=\"209\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0-1024x335.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0-300x98.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0-768x251.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0-1536x502.jpg 1536w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/Image0.jpg 2000w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>Please be sure to follow this blog series using\u202f<a href=\"https:\/\/aka.ms\/MSGraphMailbag\" target=\"_blank\" rel=\"noopener\">https:\/\/aka.ms\/MSGraphMailbag<\/a>\u202for with RSS using\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/tag\/MSGraphMailbag\">https:\/\/devblogs.microsoft.com\/microsoft365dev\/tag\/MSGraphMailbag<\/a>.<\/p>\n<h2>Introduction<\/h2>\n<p>Recently, I was reviewing questions on Microsoft Q&amp;A. I found an interesting question on <a href=\"https:\/\/docs.microsoft.com\/en-us\/answers\/questions\/687073\/how-to-differentiate-between-folders-and-files.html\">How to differentiate between folders and files<\/a>. 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 &#8220;driveItem,&#8221; this will return files, folders, pages, and news. The following is a sample query from <a href=\"https:\/\/aka.ms\/ge\">Microsoft Graph Explorer<\/a> to search for &#8220;driveItem&#8221; content.<\/p>\n<p>POST https:\/\/graph.microsoft.com\/v1.0\/search\/query<\/p>\n<div>\n<div>\n<pre class=\"prettyprint\">{\r\n  \"requests\":[\r\n    {\r\n      \"entityTypes\":[\r\n        \"driveItem\"\r\n      ],\r\n      \"query\":{\r\n        \"queryString\":\"contoso\"\r\n      }\r\n    }\r\n  ]\r\n}<\/pre>\n<\/div>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1.jpg\"><img decoding=\"async\" class=\"aligncenter size-large wp-image-9495\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1-1024x555.jpg\" alt=\"Screenshot of Search API on Microsoft Graph Explorer for driveItems\" width=\"640\" height=\"347\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1-1024x555.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1-300x163.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1-768x416.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1-1536x833.jpg 1536w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI1.jpg 1656w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<h2>Solution<\/h2>\n<\/div>\n<p>In order to filter for only files\/documents, you can add either of the following (ContentType or ContentTypeId) to the &#8220;queryString&#8221; property within the &#8220;query&#8221; element of your request.<\/p>\n<pre class=\"prettyprint\">\"query\": {\r\n  \"queryString\": \"test document <strong>AND ContentType:Document<\/strong>\"\r\n}<\/pre>\n<p>or<\/p>\n<pre class=\"prettyprint\">\"query\": {\r\n  \"queryString\": \"test document <strong>AND ContentTypeId:0x0101*<\/strong>\"\r\n}<\/pre>\n<p>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2.jpg\"><img decoding=\"async\" class=\"aligncenter size-large wp-image-9496\" src=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2-1024x506.jpg\" alt=\"Screenshot of Search API sample request on Graph Explorer filtering for only files\" width=\"640\" height=\"316\" srcset=\"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2-1024x506.jpg 1024w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2-300x148.jpg 300w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2-768x380.jpg 768w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2-1536x760.jpg 1536w, https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-content\/uploads\/sites\/73\/2022\/02\/SearchFilesViaGraphAPI2.jpg 1658w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>Here is one of the final sample queries for filtering for only files\/documents.<\/p>\n<p>POST https:\/\/graph.microsoft.com\/v1.0\/search\/query<\/p>\n<div>\n<div>\n<pre class=\"prettyprint\">{\r\n  \"requests\":[\r\n    {\r\n      \"entityTypes\":[\r\n        \"driveItem\"\r\n      ],\r\n      \"query\":{\r\n        \"queryString\":\"contoso AND ContentType:Document\"\r\n      }\r\n    }\r\n  ]\r\n}<\/pre>\n<\/div>\n<\/div>\n<h2>Conclusion<\/h2>\n<p>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.<\/p>\n<p><span data-contrast=\"none\">Today\u2019s post was written by\u00a0<\/span><span data-contrast=\"auto\">Brian T. Jackett, Principal PM with the Microsoft Graph Customer and Partner Experience (CPx) team.\u202fYou can follow Brian on Twitter <a href=\"https:\/\/twitter.com\/briantjackett\">@BrianTJackett<\/a>.\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today\u2019s Microsoft Graph Mailbag post, we\u2019ll use the Microsoft Search APIs on Microsoft Graph to return only files\/documents.<\/p>\n","protected":false},"author":73055,"featured_media":6135,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[210,153,17],"class_list":["post-9457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-graph","tag-files","tag-microsoft-search","tag-msgraphmailbag"],"acf":[],"blog_post_summary":"<p>In today\u2019s Microsoft Graph Mailbag post, we\u2019ll use the Microsoft Search APIs on Microsoft Graph to return only files\/documents.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/9457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/73055"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=9457"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/9457\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/6135"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=9457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=9457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=9457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}