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();
Related
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
I have registered an application and provided the required permission in Azure and using that application to retrieve the modified users via delta Query.
Below is the code.
var pagedCollection = await graphClient.Users
.Request()
.Delta()
.Select("userPrincipalName,mobilePhone")
.GetAsync();
But the response does not contain the nextLink or deltaLink as mentioned in the documentation.
I can get the above links if I test the API in the graph explorer.
https://graph.microsoft.com/v1.0/users/delta
Response from Graph Explorer.
Am I missing anything here while calling the same API using C#?
Any help on this will be appreciated!
You can try below code:
List<Microsoft.Graph.User> usersList = new List<Microsoft.Graph.User>();
var users = await graphClient.Users.Delta().Request().Top(10).GetAsync();
// Add the first page of results to the user list
usersList.AddRange(users.CurrentPage);
try
{
while (users.AdditionalData.ContainsKey("#odata.nextLink") && users.AdditionalData["#odata.nextLink"].ToString() != null)
{
users.InitializeNextPageRequest(graphClient, users.AdditionalData["#odata.nextLink"].ToString());
users = await users.NextPageRequest.GetAsync();
usersList.AddRange(users.CurrentPage);
}
}
catch (Exception e)
{
}
Reference link: Microsoft Graph API Pagination is not working for getting all users from Azure AD
I'm creating a team from a office 365 group using the c# sdk as specified in the documentation.
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var team = new Team
{
MemberSettings = new TeamMemberSettings
{
AllowCreateUpdateChannels = true
},
MessagingSettings = new TeamMessagingSettings
{
AllowUserEditMessages = true,
AllowUserDeleteMessages = true
},
FunSettings = new TeamFunSettings
{
AllowGiphy = true,
GiphyContentRating = GiphyRatingType.Strict
}
};
await graphClient.Groups["{id}"].Team
.Request()
.PutAsync(team);
This works to create the team and also adds the members and owners of the group automatically to the team, but it doesn't add guests from the group to the team.
Is this a known issue and is there a workaround for this problem?
Adding guest user is a two step process. First step is to send invitation for which you can use Invitation Graph API. And once user is invited to your AD you can add him/her using Add Members Graph API.
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();
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();