Last active
December 7, 2024 08:58
-
-
Save vman/39c7deb8d81b01c88b8dae5d47161327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static async Task<string> ExecuteMicrosoft365SearchWithGraph(string searchQuery) | |
{ | |
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp | |
GraphServiceClient graphClient = GetGraphClient(["User.Read", "Files.Read.All"]); | |
var requestBody = new QueryPostRequestBody | |
{ | |
Requests = new List<SearchRequest> | |
{ | |
new SearchRequest | |
{ | |
EntityTypes = new List<EntityType?> | |
{ | |
EntityType.DriveItem, | |
}, | |
Query = new SearchQuery | |
{ | |
QueryString = searchQuery, | |
}, | |
From = 0, | |
Size = 25, | |
}, | |
}, | |
}; | |
var searchResults = await graphClient.Search.Query.PostAsQueryPostResponseAsync(requestBody); | |
var result = string.Empty; | |
foreach (var hit in searchResults.Value[0].HitsContainers[0].Hits) | |
{ | |
var listItem = hit.Resource as DriveItem; | |
if (listItem != null) | |
{ | |
//Using the summary of the search result. In production, it might be the case that the summary is not enough. | |
//In that situation, the solution could be to fetch the file contents through another graph call | |
result += hit.Summary; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment