如何在Microsoft Graph Beta版日历事件API中添加自定义字段?
Great question! Microsoft Graph absolutely supports adding custom data to calendar events—you have two main options: schema extensions (structured, pre-defined) and open extensions (flexible, ad-hoc). Let’s walk through both with examples tailored to your use case.
1. Schema Extensions (Structured Custom Data)
Schema extensions let you define a structured schema for your custom data, which is ideal if you need consistent fields across multiple events or want to share this data with other apps. Even though you checked the docs, you might have missed that you first need to create the extension definition before using it in events.
Step 1: Create the Schema Extension
First, register your schema extension via a POST request to https://graph.microsoft.com/beta/schemaExtensions:
POST https://graph.microsoft.com/beta/schemaExtensions Content-Type: application/json { "id": "eventCustomData", "description": "Custom data for calendar events", "targetTypes": ["Event"], "properties": [ { "name": "projectId", "type": "String" }, { "name": "priority", "type": "Int32" } ] }
Step 2: Create an Event with the Schema Extension
Now you can include your custom fields when creating an event via https://graph.microsoft.com/beta/me/events:
POST https://graph.microsoft.com/beta/me/events Content-Type: application/json { "subject": "Team Sync Meeting", "start": { "dateTime": "2024-05-20T10:00:00", "timeZone": "Pacific Standard Time" }, "end": { "dateTime": "2024-05-20T11:00:00", "timeZone": "Pacific Standard Time" }, "eventCustomData": { "projectId": "PROJ-123", "priority": 1 } }
The response will include your custom eventCustomData object alongside the standard event fields.
2. Open Extensions (Flexible Custom Data)
If you don’t need a structured schema and want to quickly add arbitrary key-value pairs, open extensions are perfect—no pre-registration required.
Create an Event with Open Extensions
Include an extensions array in your event creation request:
POST https://graph.microsoft.com/beta/me/events Content-Type: application/json { "subject": "Client Check-In", "start": { "dateTime": "2024-05-21T14:00:00", "timeZone": "Pacific Standard Time" }, "end": { "dateTime": "2024-05-21T15:00:00", "timeZone": "Pacific Standard Time" }, "extensions": [ { "@odata.type": "#microsoft.graph.openTypeExtension", "extensionName": "com.yourcompany.eventmetadata", "clientId": "CLI-456", "notes": "Bring presentation slides" } ] }
The response will include the extensions array with your custom data.
Key Differences to Note
- Schema Extensions: Require pre-registration, enforce data types, visible to any app with the right permissions, and can be used across multiple resource types.
- Open Extensions: No pre-registration, accept any JSON data, only visible to the app that created them, and are specific to a single resource instance.
Both approaches work with the beta endpoint you’re using, so pick the one that fits your use case best!
内容的提问来源于stack exchange,提问作者atiwari

