Trying to search mail messages in C# with graph sdk

Mike Malick 1 Reputation point
2021-03-14T04:07:39.507+00:00

I'm using MSAL and the MS Graph SDK to try and learn about calling Graph API as I am new to all of this. I also have the MS Graph Explorer up to execute HTTP requests, see the results and then try to recreate those same queries in the console app using the graph client library. I've been able to duplicate most I've tried so far except for one where I am trying to search a specific user's messages for a certain subject.

I enter this into Microsoft's Graph Explorer: https://graph.microsoft.com/v1.0/users/XXX-XXXX-XXXX-XXXX-XXXX/messages?$search="subject:test" with a valid user ID. I get back the correct message in the response. I then click on the "Code snippets" tab at the bottom and then the "CSharp" sub tab and it gives me this sample C# code snippet:
+++++++++++++++++++++
GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var messages = await graphClient.Users["XXX-XXXX-XXXX-XXXX-XXXX"].Messages
.Request()
.Search("subject:test")
.GetAsync();
+++++++++++++++++++++

I have a console app already set up with all the auth and client app generation in place so I alter it to take the graph explorer generated snip and get a complie-time error: "'IUserMessagesCollectionRequest' does not contain a definition for 'Search' and no accessible extension method 'Search' accepting a first argument of type 'IUserMessagesCollectionRequest' could be found (are you missing a using directive or an assembly reference?)".

I can use Filter() instead of Search() to get the correct result, but I'm trying to understand why Search() is not present. I wasn't using beta version...just standard v1.0. Do the graph explorer generated code snippets not always generate entries that can be used in .NET client apps using the graph SDK?

77498-grapherror.jpg

Thanks in advance.

-Mike

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,569 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2021-03-15T22:29:34.97+00:00

    To use Search you need to use a custom query option see https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/overview.md#custom-query-options eg

    List<QueryOption> options = new List<QueryOption>  
    {  
         new QueryOption("$search", "subject:test")  
    };  
    var messages = await client.Me.Messages.Request(options).GetAsync();  
    

    To confuse things a litter further you can also use the Search API in graph to do the same thing https://learn.microsoft.com/en-us/graph/search-concept-messages

    0 comments No comments