Использование пакетов SDK Microsoft Graph для пакетных запросов
Мақала
Пакетная обработка — это способ объединения нескольких запросов в один HTTP-запрос. Запросы объединяются в одну полезную нагрузку JSON, которая отправляется через POST в конечную точку \$batch . Пакеты SDK Microsoft Graph имеют набор классов, упрощающих создание полезных данных пакетной службы и анализ полезных данных пакетного ответа.
Маңызды
Текущие ограничения пакетной обработки JSON в Microsoft Graph см. в разделе Известные проблемы.
Создание пакетного запроса
Пакеты SDK Для Microsoft Graph предоставляют три класса для работы с пакетными запросами и ответами.
BatchRequestStep — представляет один запрос (например, GET /me) в пакете. Он позволяет назначать уникальный идентификатор запросу и указывать зависимости между запросами.
BatchRequestContent — упрощает создание полезных данных пакетного запроса. Он содержит несколько объектов BatchRequestStep .
BatchResponseContent — упрощает синтаксический анализ ответа из пакетного запроса. Он позволяет получить все ответы, получить конкретный ответ по идентификатору и получить свойство, @odata.nextLink если оно имеется.
Автоматическая пакетная обработка для ограничений запросов
Пакет SDK для Microsoft Graph автоматически обрабатывает запросы пакетной обработки с учетом ограничения в 20 запросов на пакет. Это означает, что если код превышает это ограничение, пакет SDK разделяет запросы на отдельные пакеты в фоновом режиме. Это гарантирует, что каждый пакет соответствует ограничению. Вам больше не нужно вручную реализовывать логику для обработки этого ограничения пакетной обработки, что делает код чище и проще в управлении.
Простой пример пакетной обработки
В этом примере показано, как отправить несколько запросов в пакете, которые не зависят друг от друга. Служба может выполнять запросы в любом порядке. В этом примере получается пользователь и представление календаря пользователя на текущий день.
// Use the request builder to generate a regular// request to /mevar userRequest = graphClient.Me.ToGetRequestInformation();
var today = DateTime.Now.Date;
// Use the request builder to generate a regular// request to /me/calendarview?startDateTime="start"&endDateTime="end"var eventsRequest = graphClient.Me.CalendarView
.ToGetRequestInformation(requestConfiguration =>
{
requestConfiguration.QueryParameters.StartDateTime =
today.ToString("yyyy-MM-ddTHH:mm:ssK");
requestConfiguration.QueryParameters.EndDateTime =
today.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssK");
});
// Build the batchvar batchRequestContent = new BatchRequestContentCollection(graphClient);
// Using AddBatchRequestStepAsync adds each request as a step// with no specified order of executionvar userRequestId = await batchRequestContent
.AddBatchRequestStepAsync(userRequest);
var eventsRequestId = await batchRequestContent
.AddBatchRequestStepAsync(eventsRequest);
var returnedResponse = await graphClient.Batch.PostAsync(batchRequestContent);
// De-serialize response based on known return typetry
{
var user = await returnedResponse
.GetResponseByIdAsync<User>(userRequestId);
Console.WriteLine($"Hello {user.DisplayName}!");
}
catch (Exception ex)
{
Console.WriteLine($"Get user failed: {ex.Message}");
}
// For collections, must use the *CollectionResponse class to deserialize// The .Value property will contain the *CollectionPage type that the Graph client// returns from GetAsync().try
{
var events = await returnedResponse
.GetResponseByIdAsync<EventCollectionResponse>(eventsRequestId);
Console.WriteLine(
$"You have {events.Value?.Count} events on your calendar today.");
}
catch (Exception ex)
{
Console.WriteLine($"Get calendar view failed: {ex.Message}");
}
Go
// Use the request builder to generate a regular// request to /me
meRequest, err := graphClient.Me().
ToGetRequestInformation(context.Background(), nil)
if err != nil {
log.Fatalf("Error creating GET /me request: %v\n", err)
}
now := time.Now()
nowMidnight := time.Date(now.Year(), now.Month(), now.Day(),
0, 0, 0, 0, time.Local)
viewStart := nowMidnight.UTC().Format(time.RFC3339)
viewEnd := nowMidnight.AddDate(0, 0, 1).UTC().Format(time.RFC3339)
query := users.ItemCalendarViewRequestBuilderGetQueryParameters{
StartDateTime: &viewStart,
EndDateTime: &viewEnd,
Select: []string{"subject", "id"},
}
// Use the request builder to generate a request// to /me/calendarView?startDateTime="start"&endDateTime="end"
eventsRequest, err := graphClient.Me().
CalendarView().
ToGetRequestInformation(context.Background(),
&users.ItemCalendarViewRequestBuilderGetRequestConfiguration{
QueryParameters: &query,
})
if err != nil {
log.Fatalf("Error creating GET /me/calendarView request: %v\n", err)
}
// Build the batch
batch := graphcore.NewBatchRequest(graphClient.GetAdapter())
// Using AddBatchRequestStep adds each request as a step// with no specified order of execution
meRequestItem, err := batch.AddBatchRequestStep(*meRequest)
if err != nil {
log.Fatalf("Error adding GET /me request to batch: %v\n", err)
}
eventsRequestItem, err := batch.AddBatchRequestStep(*eventsRequest)
if err != nil {
log.Fatalf("Error adding GET /me/calendarView request to batch: %v\n", err)
}
batchResponse, err := batch.Send(context.Background(), graphClient.GetAdapter())
if err != nil {
log.Fatalf("Error sending batch: %v\n", err)
}
// De-serialize response based on known return type
user, err := graphcore.GetBatchResponseById[models.Userable](
batchResponse, *meRequestItem.GetId(), models.CreateUserFromDiscriminatorValue)
if err != nil {
log.Fatalf("Error reading GET /me response: %v\n", err)
}
fmt.Printf("Hello %s\n", *(user.GetDisplayName()))
// For collections, must use the *CollectionResponseable class to deserialize
events, err := graphcore.GetBatchResponseById[models.EventCollectionResponseable](
batchResponse, *eventsRequestItem.GetId(),
models.CreateEventCollectionResponseFromDiscriminatorValue)
if err != nil {
log.Fatalf("Error reading GET /me/calendarView response: %v\n", err)
}
fmt.Printf("You have %d events on your calendar today\n", len(events.GetValue()))
Java
// Create the batch request content with the stepsfinal BatchRequestContent batchRequestContent = new BatchRequestContent(
graphClient);
// Use the Graph client to generate the requestInformation object for GET /mefinal RequestInformation meRequestInformation = graphClient.me()
.toGetRequestInformation();
final ZoneOffset localTimeZone = OffsetDateTime.now().getOffset();
final OffsetDateTime today = OffsetDateTime.of(LocalDate.now(),
LocalTime.MIDNIGHT, localTimeZone);
final OffsetDateTime tomorrow = today.plusDays(1);
// Use the Graph client to generate the requestInformation for// GET /me/calendarView?startDateTime="start"&endDateTime="end"
RequestInformation calenderViewRequestInformation = graphClient.me()
.calendarView().toGetRequestInformation(requestConfiguration -> {
requestConfiguration.queryParameters.startDateTime = today.toString();
requestConfiguration.queryParameters.endDateTime = tomorrow.toString();
});
// Add the requestInformation objects to the batch request contentfinal String meRequestId = batchRequestContent
.addBatchRequestStep(meRequestInformation);
final String calendarViewRequestStepId = batchRequestContent
.addBatchRequestStep(calenderViewRequestInformation);
// Send the batch request content to the /$batch endpointfinal BatchResponseContent batchResponseContent = Objects.requireNonNull(
graphClient.getBatchRequestBuilder().post(batchRequestContent, null));
// Get the user response using the id assigned to the requestfinal User me = batchResponseContent.getResponseById(meRequestId,
User::createFromDiscriminatorValue);
System.out.println(String.format("Hello %s!", me.getDisplayName()));
// Get the calendar view response by idfinal EventCollectionResponse eventsResponse = Objects.requireNonNull(
batchResponseContent.getResponseById(calendarViewRequestStepId,
EventCollectionResponse::createFromDiscriminatorValue));
System.out.println(String.format("You have %d events on your calendar today",
Objects.requireNonNull(eventsResponse.getValue()).size()));
PHP
// Use the request builder to generate a GET// request to /me
$userRequest = $graphClient->me()->toGetRequestInformation();
$timeZone = new \DateTimeZone('America/New_York');
$today = new \DateTimeImmutable('today midnight', $timeZone);
$tomorrow = new \DateTimeImmutable('tomorrow midnight', $timeZone);
// Use the request builder to generate a GET// request to /me/calendarView?startDateTime="start"&endDateTime="end"
$query = new CalendarViewRequestBuilderGetQueryParameters(
startDateTime: $today->format(\DateTime::ATOM),
endDateTime: $tomorrow->format(\DateTime::ATOM));
$config = new CalendarViewRequestBuilderGetRequestConfiguration(queryParameters: $query);
$eventsRequest = $graphClient->me()->calendarView()->toGetRequestInformation($config);
// Build the batch
$userRequestItem = new BatchRequestItem($userRequest);
$eventsRequestItem = new BatchRequestItem($eventsRequest);
$batchRequestContent = new BatchRequestContent([$userRequestItem, $eventsRequestItem]);
// Create a batch request builder to send the batched requests
$batchRequestBuilder = new BatchRequestBuilder($graphClient->getRequestAdapter());
/** @var BatchResponseContent $batchResponse */
$batchResponse = $batchRequestBuilder->postAsync($batchRequestContent)->wait();
// De-serialize the responses
$user = $batchResponse->getResponseBody($userRequestItem->getId(), Models\User::class);
print('Hello '.$user->getDisplayName().'!'.PHP_EOL);
// For collections, must use the *CollectionResponse class to deserialize// getValue will return an array of items
$events = $batchResponse->getResponseBody($eventsRequestItem->getId(), Models\EventCollectionResponse::class);
print('You have '.count($events->getValue()).' events on your calendar today'.PHP_EOL);
TypeScript
// Create a batch request step to GET /me// Request is from fetch polyfill, i.e. node-fetchconst userRequestStep: BatchRequestStep = {
id: '1',
request: new Request('https://graph.microsoft.com/me', {
method: 'GET',
}),
};
// startOfToday and endOfToday from date-fnsconst start = startOfToday().toISOString();
const end = endOfToday().toISOString();
// Create a batch request step to GET// /me/calendarView?startDateTime="start"&endDateTime="end"const calendarViewRequestStep: BatchRequestStep = {
id: '2',
request: new Request(
`https://graph.microsoft.com/me/calendarView?startDateTime=${start}&endDateTime=${end}`,
{
method: 'GET',
},
),
};
// Create the batch request content with the steps created// aboveconst batchRequestContent = new BatchRequestContent([
userRequestStep,
calendarViewRequestStep,
]);
const content = await batchRequestContent.getContent();
// POST the batch request content to the /$batch endpointconst batchResponse = await graphClient.api('/$batch').post(content);
// Create a BatchResponseContent object to parse the responseconst batchResponseContent = new BatchResponseContent(batchResponse);
// Get the user response using the id assigned to the requestconst userResponse = batchResponseContent.getResponseById('1');
// For a single entity, the JSON payload can be deserialized// into the expected type// Types supplied by @microsoft/microsoft-graph-typesif (userResponse.ok) {
const user: User = (await userResponse.json()) as User;
console.log(`Hello ${user.displayName}!`);
} else {
console.log(`Get user failed with status ${userResponse.status}`);
}
// Get the calendar view response by idconst calendarResponse = batchResponseContent.getResponseById('2');
// For a collection of entities, the "value" property of// the JSON payload can be deserialized into an array of// the expected typeif (calendarResponse.ok) {
const rawResponse = (await calendarResponse.json()) as PageCollection;
const events: Event[] = rawResponse.value;
console.log(`You have ${events.length} events on your calendar today.`);
} else {
console.log(
`Get calendar view failed with status ${calendarResponse.status}`,
);
}
Пакеты с зависимыми запросами
В этом примере показано, как отправить несколько запросов в пакете, которые зависят друг от друга. Служба выполняет запрос в порядке, указанном зависимостями. В этом примере в календарь пользователя добавляется событие с временем начала в течение текущего дня и возвращается представление календаря пользователя за текущий день. Чтобы убедиться, что возвращенная проверка календаря включает созданное событие, запрос для представления календаря настраивается как зависящий от запроса на добавление нового события. Это гарантирует, что запрос на добавление события выполняется первым.
Ескерім
Если запрос на добавление события завершается ошибкой, запрос на получение представления календаря завершается ошибкой 424 Failed Dependency .
var today = DateTime.Now.Date;
var newEvent = new Event
{
Subject = "File end-of-day report",
Start = new DateTimeTimeZone
{
// 5:00 PM
DateTime = today.AddHours(17)
.ToString("yyyy-MM-ddTHH:mm:ss"),
TimeZone = TimeZoneInfo.Local.StandardName,
},
End = new DateTimeTimeZone
{
// 5:30 PM
DateTime = today.AddHours(17).AddMinutes(30)
.ToString("yyyy-MM-ddTHH:mm:ss"),
TimeZone = TimeZoneInfo.Local.StandardName,
},
};
// Use the request builder to generate a regular// POST request to /me/eventsvar addEventRequest = graphClient.Me.Events
.ToPostRequestInformation(newEvent);
// Use the request builder to generate a regular// request to /me/calendarview?startDateTime="start"&endDateTime="end"var calendarViewRequest = graphClient.Me.CalendarView.ToGetRequestInformation(
requestConfiguration =>
{
requestConfiguration.QueryParameters.StartDateTime =
today.ToString("yyyy-MM-ddTHH:mm:ssK");
requestConfiguration.QueryParameters.EndDateTime =
today.AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssK");
});
// Build the batchvar batchRequestContent = new BatchRequestContentCollection(graphClient);
// Force the requests to execute in order, so that the request for// today's events will include the new event created.// First request, no dependencyvar addEventRequestId = await batchRequestContent
.AddBatchRequestStepAsync(addEventRequest);
// Second request, depends on addEventRequestIdvar eventsRequestId = Guid.NewGuid().ToString();
var eventsRequestMessage = await graphClient.RequestAdapter
.ConvertToNativeRequestAsync<HttpRequestMessage>(calendarViewRequest);
batchRequestContent.AddBatchRequestStep(new BatchRequestStep(
eventsRequestId,
eventsRequestMessage,
[addEventRequestId]));
var returnedResponse = await graphClient.Batch.PostAsync(batchRequestContent);
// De-serialize response based on known return typetry
{
var createdEvent = await returnedResponse
.GetResponseByIdAsync<Event>(addEventRequestId);
Console.WriteLine($"New event created with ID: {createdEvent.Id}");
}
catch (Exception ex)
{
Console.WriteLine($"Add event failed: {ex.Message}");
}
// For collections, must use the *CollectionResponse class to deserialize// The .Value property will contain the *CollectionPage type that the Graph client// returns from GetAsync().try
{
var events = await returnedResponse
.GetResponseByIdAsync<EventCollectionResponse>(eventsRequestId);
Console.WriteLine(
$"You have {events.Value?.Count} events on your calendar today.");
}
catch (Exception ex)
{
Console.WriteLine($"Get calendar view failed: {ex.Message}");
}
Go
now := time.Now()
nowMidnight := time.Date(now.Year(), now.Month(), now.Day(),
0, 0, 0, 0, time.Local)
timeZone, _ := tzlocal.RuntimeTZ()
// 5:00 PM
startDateTime := nowMidnight.Add(time.Hour * 17)
// 5:30 PM
endDateTime := startDateTime.Add(time.Minute * 30)
graphDateTimeFormat := "2006-01-02T15:04:05"// Create event
newEvent := models.NewEvent()
subject := "File end-of-day report"
newEvent.SetSubject(&subject)
start := models.NewDateTimeTimeZone()
startString := startDateTime.Format(graphDateTimeFormat)
start.SetDateTime(&startString)
start.SetTimeZone(&timeZone)
newEvent.SetStart(start)
end := models.NewDateTimeTimeZone()
endString := endDateTime.Format(graphDateTimeFormat)
end.SetDateTime(&endString)
end.SetTimeZone(&timeZone)
newEvent.SetEnd(end)
addEventRequest, err := graphClient.Me().
Events().
ToPostRequestInformation(context.Background(), newEvent, nil)
if err != nil {
log.Fatalf("Error creating POST /me/events request: %v\n", err)
}
viewStart := nowMidnight.UTC().Format(time.RFC3339)
viewEnd := nowMidnight.AddDate(0, 0, 1).UTC().Format(time.RFC3339)
query := users.ItemCalendarViewRequestBuilderGetQueryParameters{
StartDateTime: &viewStart,
EndDateTime: &viewEnd,
Select: []string{"subject", "id"},
}
// Use the request builder to generate a request// to /me/calendarView?startDateTime="start"&endDateTime="end"
eventsRequest, err := graphClient.Me().
CalendarView().
ToGetRequestInformation(context.Background(),
&users.ItemCalendarViewRequestBuilderGetRequestConfiguration{
QueryParameters: &query,
})
if err != nil {
log.Fatalf("Error creating GET /me/calendarView request: %v\n", err)
}
// Build the batch
batch := graphcore.NewBatchRequest(graphClient.GetAdapter())
// Force the requests to execute in order, so that the request for// today's events will include the new event created.// First request, no dependency
addEventRequestItem, err := batch.AddBatchRequestStep(*addEventRequest)
if err != nil {
log.Fatalf("Error adding POST /me/events request to batch: %v\n", err)
}
// Second request, depends on addEventRequestId
eventsRequestItem, err := batch.AddBatchRequestStep(*eventsRequest)
if err != nil {
log.Fatalf("Error creating GET /me/calendarView request to batch: %v\n", err)
}
eventsRequestItem.DependsOnItem(addEventRequestItem)
batchResponse, err := batch.Send(context.Background(), graphClient.GetAdapter())
if err != nil {
log.Fatalf("Error sending batch: %v\n", err)
}
// De-serialize response based on known return type
event, err := graphcore.GetBatchResponseById[models.Eventable](
batchResponse, *addEventRequestItem.GetId(),
models.CreateEventFromDiscriminatorValue)
if err != nil {
log.Fatalf("Error reading POST /me/events response: %v\n", err)
}
fmt.Printf("New event created with ID: %s\n", *(event.GetId()))
// For collections, must use the *CollectionResponseable class to deserialize
events, err := graphcore.GetBatchResponseById[models.EventCollectionResponseable](
batchResponse, *eventsRequestItem.GetId(),
models.CreateEventCollectionResponseFromDiscriminatorValue)
if err != nil {
log.Fatalf("Error reading GET /me/calendarView response: %v\n", err)
}
fmt.Printf("You have %d events on your calendar today\n", len(events.GetValue()))
Java
// Create the batch request content with the stepsfinal BatchRequestContent batchRequestContent = new BatchRequestContent(
graphClient);
final ZoneOffset localTimeZone = OffsetDateTime.now().getOffset();
final OffsetDateTime today = OffsetDateTime.of(LocalDate.now(),
LocalTime.MIDNIGHT, localTimeZone);
final OffsetDateTime tomorrow = today.plusDays(1);
// Create a new event for today at 5:00 PMfinal Event newEvent = new Event();
newEvent.setSubject("File end-of-day report");
// 5:00 PMfinal DateTimeTimeZone start = new DateTimeTimeZone();
start.setDateTime(
today.plusHours(17).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
start.setTimeZone(ZoneOffset.systemDefault().getId());
newEvent.setStart(start);
// 5:30 PMfinal DateTimeTimeZone end = new DateTimeTimeZone();
end.setDateTime(today.plusHours(17).plusMinutes(30)
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
end.setTimeZone(ZoneOffset.systemDefault().getId());
newEvent.setEnd(end);
// Use the Graph client to add the requestInformation for POST /me/events
RequestInformation postEventRequestInformation = graphClient.me().events()
.toPostRequestInformation(newEvent);
// Get the id assigned to the request
String postEventRequestId = batchRequestContent
.addBatchRequestStep(postEventRequestInformation);
// Use the Graph client to generate the requestInformation// GET /me/calendarView?startDateTime="start"&endDateTime="end"final RequestInformation calendarViewRequestInformation = graphClient.me()
.calendarView().toGetRequestInformation(requestConfiguration -> {
requestConfiguration.queryParameters.startDateTime = today.toString();
requestConfiguration.queryParameters.endDateTime = tomorrow.toString();
});
final String calendarViewRequestId = batchRequestContent
.addBatchRequestStep(calendarViewRequestInformation);
// Set the dependsOnId to 'postEventRequestId'
batchRequestContent.getBatchRequestSteps().get(calendarViewRequestId)
.addDependsOnId(postEventRequestId);
// Send the batch request content to the /$batch endpointfinal BatchResponseContent batchResponseContent = Objects.requireNonNull(
graphClient.getBatchRequestBuilder().post(batchRequestContent, null));
// Get the event response using the id assigned to the requestfinal Event postedEvent = batchResponseContent.getResponseById(postEventRequestId,
Event::createFromDiscriminatorValue);
System.out.println(String.format("New event created with ID: %s",
Objects.requireNonNull(postedEvent.getId())));
// Get the calendar view response by idfinal EventCollectionResponse eventsResponse = Objects
.requireNonNull(batchResponseContent.getResponseById(calendarViewRequestId,
EventCollectionResponse::createFromDiscriminatorValue));
System.out.println(String.format("You have %d events on your calendar today",
Objects.requireNonNull(eventsResponse.getValue().size())));
PHP
$startTime = new \DateTimeImmutable('today 5PM');
$endTime = $startTime->add(new \DateInterval('PT30M'));
$newEvent = new Models\Event();
$newEvent->setSubject('File end-of-day report');
$start = new Models\DateTimeTimeZone();
$start->setDateTime($startTime->format('Y-m-d\TH:i:s'));
$start->setTimeZone('Eastern Standard Time');
$newEvent->setStart($start);
$end = new Models\DateTimeTimeZone();
$end->setDateTime($endTime->format('Y-m-d\TH:i:s'));
$end->setTimeZone('Eastern Standard Time');
$newEvent->setEnd($end);
// Use the request builder to generate a// POST request to /me/events
$addEventRequest = $graphClient->me()->events()->toPostRequestInformation($newEvent);
$timeZone = new \DateTimeZone('America/New_York');
$today = new \DateTimeImmutable('today midnight', $timeZone);
$tomorrow = new \DateTimeImmutable('tomorrow midnight', $timeZone);
// Use the request builder to generate a GET// request to /me/calendarView?startDateTime="start"&endDateTime="end"
$query = new CalendarViewRequestBuilderGetQueryParameters(
startDateTime: $today->format(\DateTime::ATOM),
endDateTime: $tomorrow->format(\DateTime::ATOM));
$config = new CalendarViewRequestBuilderGetRequestConfiguration(queryParameters: $query);
$eventsRequest = $graphClient->me()->calendarView()->toGetRequestInformation($config);
// Build the batch// Force the requests to execute in order, so that the request for// today's events will include the new event created.// First request, no dependency
$addEventRequestItem = new BatchRequestItem($addEventRequest);
// Second request, depends on addEventRequestItem
$eventsRequestItem = new BatchRequestItem($eventsRequest, dependsOn: [$addEventRequestItem]);
$batchRequestContent = new BatchRequestContent([$addEventRequestItem, $eventsRequestItem]);
// Create a batch request builder to send the batched requests
$batchRequestBuilder = new BatchRequestBuilder($graphClient->getRequestAdapter());
/** @var BatchResponseContent $batchResponse */
$batchResponse = $batchRequestBuilder->postAsync($batchRequestContent)->wait();
// De-serialize the responses
$createdEvent = $batchResponse->getResponseBody($addEventRequestItem->getId(), Models\Event::class);
print('New event created with ID: '.$createdEvent->getId().PHP_EOL);
// For collections, must use the *CollectionResponse class to deserialize// getValue will return an array of items
$events = $batchResponse->getResponseBody($eventsRequestItem->getId(), Models\EventCollectionResponse::class);
print('You have '.count($events->getValue()).' events on your calendar today'.PHP_EOL);
TypeScript
// 5:00 PM today// startOfToday, endOfToday, setHours, setMinutes, format from date-fnsconst eventStart = setHours(startOfToday(), 17);
const eventEnd = setMinutes(eventStart, 30);
// Create a batch request step to add an eventconst newEvent: Event = {
subject: 'File end-of-day report',
start: {
dateTime: format(eventStart, `yyyy-MM-dd'T'HH:mm:ss`),
timeZone: 'Pacific Standard Time',
},
end: {
// 5:30 PM
dateTime: format(eventEnd, `yyyy-MM-dd'T'HH:mm:ss`),
timeZone: 'Pacific Standard Time',
},
};
// Request is from fetch polyfill, i.e. node-fetchconst addEventRequestStep: BatchRequestStep = {
id: '1',
request: new Request('https://graph.microsoft.com/me/events', {
method: 'POST',
body: JSON.stringify(newEvent),
headers: {
'Content-Type': 'application/json',
},
}),
};
const start = startOfToday().toISOString();
const end = endOfToday().toISOString();
// Create a batch request step to GET// /me/calendarView?startDateTime="start"&endDateTime="end"const calendarViewRequestStep: BatchRequestStep = {
id: '2',
// This step will happen after step 1
dependsOn: ['1'],
request: new Request(
`https://graph.microsoft.com/me/calendarView?startDateTime=${start}&endDateTime=${end}`,
{
method: 'GET',
},
),
};
// Create the batch request content with the steps created// aboveconst batchRequestContent = new BatchRequestContent([
addEventRequestStep,
calendarViewRequestStep,
]);
const content = await batchRequestContent.getContent();
// POST the batch request content to the /$batch endpointconst batchResponse = await graphClient.api('/$batch').post(content);
// Create a BatchResponseContent object to parse the responseconst batchResponseContent = new BatchResponseContent(batchResponse);
// Get the create event response by idconst newEventResponse = batchResponseContent.getResponseById('1');
if (newEventResponse.ok) {
const event: Event = (await newEventResponse.json()) as Event;
console.log(`New event created with ID: ${event.id}`);
} else {
console.log(`Create event failed with status ${newEventResponse.status}`);
}
// Get the calendar view response by idconst calendarResponse = batchResponseContent.getResponseById('2');
if (calendarResponse.ok) {
// For a collection of entities, the "value" property of// the JSON payload can be deserialized into an array of// the expected typeconst rawResponse = (await calendarResponse.json()) as PageCollection;
const events: Event[] = rawResponse.value;
console.log(`You have ${events.length} events on your calendar today.`);
} else {
console.log(
`Get calendar view failed with status ${calendarResponse.status}`,
);
}
Microsoft Graph предоставляет доступ к данным, хранящимся в службах Microsoft 365. В этом модуле вы узнаете, как получить доступ к данным Microsoft 365, чтобы отображать стандартный календарь пользователя в приложении JavaScript с помощью пакетов SDK и API Microsoft Graph. Чтобы в веб-приложении отображалось только то, что непосредственно относится к пользователю, вы получите доступ к экземплярам событий за указанный период.