Namespace: microsoft.graph
Search the hierarchy of items for items matching a query.
You can search within a folder hierarchy, a whole drive, or files shared with the current user.
Permissions
Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.
| Permission type |
Least privileged permissions |
Higher privileged permissions |
| Delegated (work or school account) |
Files.Read |
Files.Read.All, Files.ReadWrite, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All |
| Delegated (personal Microsoft account) |
Files.Read |
Files.Read.All, Files.ReadWrite, Files.ReadWrite.All |
| Application |
Files.Read.All |
Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All |
Note: This method does not support the Sites.Selected application permission.
HTTP request
GET /drives/{drive-id}/root/search(q='{search-text}')
GET /groups/{group-id}/drive/root/search(q='{search-text}')
GET /me/drive/root/search(q='{search-text}')
GET /sites/{site-id}/drive/root/search(q='{search-text}')
GET /users/{user-id}/drive/root/search(q='{search-text}')
Optional query parameters
This method supports the $expand, $select, $skipToken, $top, and $orderby OData query parameters to customize the response.
Function parameters
| Parameter |
Type |
Description |
| q |
string |
The query text used to search for items. Values may be matched across several fields including filename, metadata, and file content. |
Example
Request
The following example searches for a match for "Contoso Project" across several fields in the signed-in user's drive items.
GET /me/drive/root/search(q='Contoso Project')
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
q := "{q}"
search, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").SearchWithQ(&q).GetAsSearchWithQGetResponse(context.Background(), nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").searchWithQ("{q}").get();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/root/search(q='Contoso Project')')
.get();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->searchWithQ('{q}', )->get()->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').search_with_q("{q}").get()
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Response
This method returns an object containing an collection of DriveItems that match the search criteria.
If no items were found, an empty collection is returned.
If there are too many matches the response will be paged and an @odata.nextLink property will contain a URL to the next page of results.
You can use the $top query parameter to specify the number of items in the page.
HTTP/1.1 200 OK
Content-type: application/json
{
"value": [
{
"id": "0123456789abc!123",
"name": "Contoso Project",
"folder": {},
"searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!123" }
},
{
"id": "0123456789abc!456",
"name": "Contoso Project 2016",
"folder": {},
"searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!456" }
}
],
"@odata.nextLink": "https://graph.microsoft.com/v1.0/me/drive/root/search(query='contoso project')&skipToken=1asdlnjnkj1nalkm!asd"
}
Searching for items a user can access
In addition to searching for items within a drive, your app can search more broadly to include items shared with the current user.
To broaden the search scope, use the search method on the Drive resource.
Example
GET /me/drive/search(q='Contoso Project')
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].SearchWithQ("{q}").GetAsSearchWithQGetResponseAsync();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
q := "{q}"
search, err := graphClient.Drives().ByDriveId("drive-id").SearchWithQ(&q).GetAsSearchWithQGetResponse(context.Background(), nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
var result = graphClient.drives().byDriveId("{drive-id}").searchWithQ("{q}").get();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
const options = {
authProvider,
};
const client = Client.init(options);
let search = await client.api('/me/drive/search(q='Contoso Project')')
.get();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
<?php
use Microsoft\Graph\GraphServiceClient;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->searchWithQ('{q}', )->get()->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.drives.by_drive_id('drive-id').search_with_q("{q}").get()
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.
Response
Responses when searching from the drive resource may include items outside of the drive (items shared with the current user).
These items include the remoteItem facet to indicate they are stored outside of the target drive.
HTTP/1.1 200 OK
Content-type: application/json
{
"value": [
{
"id": "0123456789abc!123",
"name": "Contoso Project",
"folder": {},
"searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!123" },
"remoteItem": { "id": "!23141901", "parentReference": { "driveId": "s!1020101jlkjl12lx" } }
},
{
"id": "0123456789abc!456",
"name": "Contoso Project 2016",
"folder": {},
"searchResult": { "onClickTelemetryUrl": "https://bing.com/0123456789abc!456" }
}
],
"@odata.nextLink": "https://graph.microsoft.com/v1.0/me/drive/root/search(query='contoso project')&skipToken=1asdlnjnkj1nalkm!asd"
}
Error responses
For more information about how errors are returned, see Error responses.