GraphServiceClient Select control not working as expected - c#

The following piece of code returns a lot more fields than the ones I´ve specified.
var user = await graphClient.Users["{user-id}"]
.Request()
.Select(u => new {
u.DisplayName,
u.JobTitle,
u.GivenName,
u.PostalCode,
u.Identities
})
.GetAsync();
I am following the instructions from ms docs stated here. What am I missing?

Related

How to filter Email Messages by Subject Body & HasAttachments using C# Graph API

var inboxMessages = await graphClient.Me
.MailFolders["Inbox"]
.Messages
.Request()
.Select("sender,subject")
.Top(5)
.GetAsync();
I want to filter the messages by subject and hasAttachment using C#
I am seeing few examples like this ,but how we need implement this in C# in above code?
/v1.0/me/messages?$search="subject:search term"
/v1.0/me/messages?$filter=contains(subject, 'my search term')
Can anyone help me out on this.
You can filter messages with attachments based on the subject that contains specific text like this
var subjectText = "your text";
var inboxMessages = await graphClient.Me
.MailFolders["Inbox"]
.Messages
.Request()
.Select("sender,subject")
.Filter($"hasAttachments eq true and contains(subject,'{subjectText}')")
.Top(5)
.GetAsync();
Documentation:
$filter query operator

Include search in Microsoft graph query C#

How do I run the query
https://graph.microsoft.com/v1.0/users?$count=true&$search="displayName:room"&$filter=endsWith(mail,'xxxx.com')&$select=id,displayName,mail
in C#?
this is what I have now:
return await _graphServiceClient
.Users.Request()
.Header("ConsistencyLevel", "eventual")
. .Filter($"(endsWith(mail, 'xxxx.com'))&$count=true")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
Try this code pls:
var queryOptions = new List<QueryOption>()
{
new QueryOption("$count", "true"),
new QueryOption("$search", "\"displayName:tiny\"")
};
var res = await graphClient
.Users.Request(queryOptions)
.Header("ConsistencyLevel", "eventual")
.Filter("endswith(mail,'contoso.com')")
.OrderBy("userPrincipalName")
.Select("id,displayName,mail")
.Top(999)
.GetAsync();
When we follow the official code snippet, we should use .Search() but it will meet exception:
Then let's see github issue here, and we can set search parameter into query option.

Querying Azure AD using Graph API

I'm trying to retrieve all users for a given AD domain. Whilst I have managed to retrieve the user list successfully, the next step is to identify which groups the user is a member of. This is where it gets hard.
Step 1)
var clientSecretCredential = new ClientSecretCredential(configuration.TenantId, configuration.ClientId, ClientSecret);
GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential);
return await Task.FromResult(graphClient);
This gets me a successful connection to the GraphClient
var users = await graphClient.Users.Request().GetAsync();
foreach (var user in users)
{
Console.WriteLine(user.DisplayName);
}
displays the list of users (using their DisplayName)
Step 2)
var groups = await graphClient.Users[user.Id].TransitiveMemberOf.Request().GetAsync();
This gets me the user's groups. Finally I would like to display the actual group's name....and this is where it fails. I am able to iterate over around groups but the only available properties are things like 'id' there is no DisplayName property.
Any help here would be appreciated.
Could you please try to run the sample code :
var page = await graphClient
.Users[userObjectId]
.MemberOf
.Request()
.GetAsync();
var names = new List<string>();
names.AddRange(page
.OfType<Group>()
.Select(x => x.DisplayName)
.Where(name => !string.IsNullOrEmpty(name)));
while (page.NextPageRequest != null)
{
page = await page.NextPageRequest.GetAsync();
names.AddRange(page
.OfType<Group>()
.Select(x => x.DisplayName)
.Where(name => !string.IsNullOrEmpty(name)));
}
return names;
Sample question - How to get group names of the user is a member of using Microsoft Graph API?
Hope this helps.
Thanks

Microsoft Graph Get All Users Exception Unsupported Query

I can submit this query to Microsoft Graph Explorer but in C# I get the an error message: "Microsoft.Graph.ServiceException: "Code: Request_UnsupportedQuery
Message: Unsupported Query."
var users = await graphClient.Users
.Request()
.Filter("endswith(mail,'#mydomain.com')")
.OrderBy("userPrincipalName")
.GetAsync();
You should send a header ConsistencyLevel=eventual and also $count query parameter to make it work.
To add $count query parameter you can use queryOptions.
List<QueryOption> queryOptions = new List<QueryOption>
{
new QueryOption("$count", true)
};
var users = await graphClient.Users
.Request(queryOptions)
.Filter("endswith(mail,'#mydomain.com')")
.OrderBy("userPrincipalName")
.GetAsync();
The API call what look something like this
https://graph.microsoft.com/v1.0/users?$count=true&$filter=endswith(mail, '#domain.live')&$orderBy=userPrincipalName
You can always test these calls in Graph Explorer.

QueryOptions ignored when getting calendar events in MSGraph Dot Net

I'm trying to get the user's calendar events for today. So I added some query parameters but they're getting ignored and the graph client returns the user's events as if I didn't supply any parameters (startatetime):
var options = new QueryOption[]
{
new QueryOption("startdatetime", DateTime.UtcNow.ToString("o")),
new QueryOption("enddatetime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.Calendar
.Events
.Request(options)
.GetAsync();
I tested it in the graph explorer and it works fine. But in the sdk, it returns calendar events that started before today.
Your code is the equivalent of calling:
`/events?startdatetime={dateTime}&enddatetime={dateTime}`.
That is a valid endpoint, but you're passing invalid query params. What you're looking for is calendarView:
`/calendarView?startdatetime={dateTime}&enddatetime={dateTime}`
Using the SDK, this would look like this:
var options = new QueryOption[]
{
new QueryOption("startDateTime", DateTime.UtcNow.ToString("o")),
new QueryOption("endDateTime", DateTime.UtcNow.AddDays(1).ToString("o")),
};
var events = await graphServiceClient
.Me
.CalendarView
.Request(options)
.GetAsync();

Categories