QueryOptions ignored when getting calendar events in MSGraph Dot Net - c#

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();

Related

Copy a mail using Microsoft Graph C# SDK

I want to copy a mail from one folder to another.
Referring to the documentation, it should work like this:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Me.Messages["{message-id}"]
.Copy(destinationId)
.Request()
.PostAsync();
However, when I try to use .Copy() like described I get the error, that I can't use it like a method. If I try to add the information as an [], like it's done for the users or messages property, I receive a different error.
I have tried it like this:
var graphClient = GetGraphClientInstance();
var destinationFolderId = "destinationFolderId-value";
await graphClient.Users["myUserName"].Messages[specificMail.Id]
.Copy(destinationFolderId )
.Request()
.PostAsync();
I'm using Microsoft.Graph version 5.0.0-preview-12.
Does anyone have an idea how to use the Copy property correctly?
Using Microsoft.Graph v5 your original code:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Me.Messages["{message-id}"]
.Copy(destinationId)
.Request()
.PostAsync();
should be changed to:
var graphClient = new GraphServiceClient(authProvider);
var destinationId = "destinationId-value";
await graphClient.Users["myUserName"].Messages[specificMail.Id]
.MicrosoftGraphCopy
.PostAsync(new CopyPostRequestBody { DestinationId = destinationId });

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.

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.

C# Microsoft Graph API obtain specific calendar

I'm using the new Microsoft Graph Core API v1.20.0 in a Visual Studio 2019 v16.4.5 C# project.
I want to obtain all calendar entries (Events) of each office365 calendar separately.
So far I know, I can retrieve all calendars with await graphClient.Me.Calendars.Request().GetAsync(); and Events with
string startDate = DateTime.Now.AddMonths(-1).ToString("s");
string endDate = DateTime.Now.AddYears(1).ToString("s");
var queryOptions = new List<QueryOption>()
{
new QueryOption("startDateTime", startDate),
new QueryOption("endDateTime", endDate),
};
var resultPage = await graphClient.Me.Events.Request(queryOptions)
.OrderBy("createdDateTime DESC")
.GetAsync();
But I don't know how to iterate over all Calendars and obtaining all Events for each Calendar.
Me.Events will just return events from the main calendar for a User.
You will need to use Me.Calendars first as documented here https://learn.microsoft.com/en-us/graph/api/user-list-calendars?view=graph-rest-1.0&tabs=http
var calendars = await graphClient.Me.Calendars
.Request()
.GetAsync();
and for each calendar id you get back call list events https://learn.microsoft.com/en-us/graph/api/user-list-events?view=graph-rest-1.0&tabs=http
GET /me/calendars/{id}/events
which in the SDK would be
graphClient.Me.Calendars["calendar-id"].Events..Request()
.GetAsync();

MS Graph SDK: Adding Parameters to CalendarView

I have the following statement in Graph Rest API that returns all events from a certain calendar within the given DateTime:
https://graph.microsoft.com/beta/me/calendars/ID/calendarView?startDateTime=2017-02-01T10:31:37Z&endDateTime=2017-02-10T10:31:37Z
How would I do this using the SDK? What I got so far:
ICalendarEventsCollectionPage retrievedEvent = await graphClient.Me.Calendars[id].CalendarView...
You can add these as QueryOptions to the request.
QueryOption startDateTime = new QueryOption("startDateTime", "2017-02-01T10:31:37Z");
QueryOption endDateTime = new QueryOption("endDateTime", "2017-02-10T10:31:37Z");
List options = new List();
options.Add(startDateTime);
options.Add(endDateTime);
ICalendarCalendarViewCollectionPage retrievedEvents = await graphClient
.Me
.Calendars["id"]
.CalendarView
.Request(options)
.GetAsync();

Categories