Dave
Dave

Reputation: 531

Search Users within Graph API

In my C# application I am trying to search for a User via Graph API. The only parameter I have is the username which is stored in onPremisesSamAccountName field.

Through Graph Explorer I can successfully run the query

https://graph.microsoft.com/v1.0/users?$count=true&$search="onPremisesSamAccountName:myusername"&$select=id,displayName

And Graph Explorer gives me the C# code to use

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var users = await graphClient.Users
    .Request()
    .Search("onPremisesSamAccountName:myusername")
    .Select("id,displayName")
    .GetAsync();

Now when I try to use that code I get an error saying that Search is not a method, do I need to add an extra package to use Search?

Upvotes: 1

Views: 1757

Answers (1)

user2250152
user2250152

Reputation: 20635

I also didn't find any nuget package with Search method.

You can specify search value by using query option. $search query parameter requires a request header ConsistencyLevel: eventual

var queryOptions = new List<Option>()
        {
            new QueryOption("$search", "\"onPremisesSamAccountName:myusername\""),
            new HeaderOption("ConsistencyLevel", "eventual")
        };
        var users = await graphClient.Users
            .Request(queryOptions)
            .Select("id,displayName")
            .GetAsync();

Upvotes: 5

Related Questions