You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Graph API日历事件端点是否支持Adaptive Card附件及.NET实现方案

问题解答:Graph API日历事件添加Adaptive Card附件及.NET SDK实现

一、日历邀请端点是否支持Adaptive Card?

Microsoft Graph API的/me/calendar/events端点支持添加Adaptive Card作为附件,但必须遵循正确的请求格式要求。

二、解决当前的反序列化错误

你遇到的UnableToDeserializePostBody错误,核心原因有两个:

  1. Adaptive Card的content字段格式错误:请求体中将Adaptive Card的JSON转成了转义字符串传入,但Graph API要求该字段直接传入原始JSON对象,而非转义后的字符串。
  2. subject字段不能为空:请求体中subject设为null,这不符合API要求,必须提供有效值(即使是空字符串也不能传null)。

修正后的请求体示例

{
  "subject": "带Adaptive Card的会议邀请",
  "body": {
    "contentType": "html",
    "content": "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"
  },
  "attachments": [
    {
      "id": "74d20c7f34aa4a7fb74e2b30004247c5",
      "contentType": "application/vnd.microsoft.card.adaptive",
      "contentUrl": null,
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "version": "1.5",
        "type": "AdaptiveCard",
        "body": [
          {
            "type": "Container",
            "style": "emphasis",
            "items": [
              {
                "type": "TextBlock",
                "text": "The EY organization today announces the details of the 52 women founders",
                "wrap": true
              },
              {
                "type": "ColumnSet",
                "columns": [
                  {
                    "style": "default",
                    "width": "stretch",
                    "items": [
                      {
                        "type": "ColumnSet",
                        "columns": [
                          {
                            "items": [
                              {
                                "type": "Image",
                                "width": "200px",
                                "url": "https://tse3.mm.bing.net/th/id/OIP._awI5wQIVTy1k8QdHtpSWQHaEK?w=292&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7"
                              }
                            ],
                            "type": "Column",
                            "width": "auto"
                          },
                          {
                            "items": [
                              {
                                "type": "TextBlock",
                                "size": "Medium",
                                "weight": "Bolder",
                                "text": "Some Text",
                                "wrap": true
                              },
                              {
                                "type": "TextBlock",
                                "size": "Medium",
                                "weight": "Bolder",
                                "text": "Adaptive Cards 介绍",
                                "wrap": true
                              }
                            ],
                            "type": "Column",
                            "width": "auto"
                          }
                        ]
                      }
                    ],
                    "minHeight": "100px",
                    "type": "Column"
                  }
                ]
              }
            ]
          }
        ]
      },
      "name": null,
      "thumbnailUrl": null
    }
  ],
  "start": {
    "dateTime": "2022-11-18T12:00:00",
    "timeZone": "Pacific Standard Time"
  },
  "end": {
    "dateTime": "2022-11-18T12:30:00",
    "timeZone": "Pacific Standard Time"
  },
  "location": {
    "displayName": "Harry's Bar"
  },
  "attendees": [
    {
      "emailAddress": {
        "address": "xero.01234@contoso.com",
        "name": "Orex"
      },
      "type": "required"
    }
  ],
  "isOnlineMeeting": true,
  "onlineMeetingProvider": "teamsForBusiness"
}

三、通过.NET SDK向Outlook邮件/事件发送Adaptive Card

1. 安装依赖NuGet包

Install-Package Microsoft.Graph
Install-Package Azure.Identity

2. 创建带Adaptive Card附件的日历事件示例

using Microsoft.Graph;
using Azure.Identity;
using System.Text.Json;

// 配置身份验证参数
var scopes = new[] { "Calendars.ReadWrite" };
var clientId = "你的应用客户端ID";
var tenantId = "你的租户ID";
var clientSecret = "你的应用客户端密钥";

// 初始化Graph客户端
var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(credential, scopes);

// 定义Adaptive Card内容
var adaptiveCard = new
{
    "$schema" = "http://adaptivecards.io/schemas/adaptive-card.json",
    version = "1.5",
    type = "AdaptiveCard",
    body = new[]
    {
        new
        {
            type = "Container",
            style = "emphasis",
            items = new[]
            {
                new
                {
                    type = "TextBlock",
                    text = "The EY organization today announces the details of the 52 women founders",
                    wrap = true
                },
                new
                {
                    type = "ColumnSet",
                    columns = new[]
                    {
                        new
                        {
                            style = "default",
                            width = "stretch",
                            items = new[]
                            {
                                new
                                {
                                    type = "ColumnSet",
                                    columns = new[]
                                    {
                                        new
                                        {
                                            items = new[]
                                            {
                                                new
                                                {
                                                    type = "Image",
                                                    width = "200px",
                                                    url = "https://tse3.mm.bing.net/th/id/OIP._awI5wQIVTy1k8QdHtpSWQHaEK?w=292&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7"
                                                }
                                            },
                                            type = "Column",
                                            width = "auto"
                                        },
                                        new
                                        {
                                            items = new[]
                                            {
                                                new
                                                {
                                                    type = "TextBlock",
                                                    size = "Medium",
                                                    weight = "Bolder",
                                                    text = "Some Text",
                                                    wrap = true
                                                },
                                                new
                                                {
                                                    type = "TextBlock",
                                                    size = "Medium",
                                                    weight = "Bolder",
                                                    text = "Adaptive Cards 介绍",
                                                    wrap = true
                                                }
                                            },
                                            type = "Column",
                                            width = "auto"
                                        }
                                    }
                                }
                            },
                            minHeight = "100px",
                            type = "Column"
                        }
                    }
                }
            }
        }
    }
};

// 序列化Adaptive Card为JSON字符串
var adaptiveCardJson = JsonSerializer.Serialize(adaptiveCard);

// 构建日历事件对象
var newEvent = new Event
{
    Subject = "带Adaptive Card的会议邀请",
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "<attachment id=\"74d20c7f34aa4a7fb74e2b30004247c5\"></attachment>"
    },
    Start = new DateTimeTimeZone { DateTime = "2022-11-18T12:00:00", TimeZone = "Pacific Standard Time" },
    End = new DateTimeTimeZone { DateTime = "2022-11-18T12:30:00", TimeZone = "Pacific Standard Time" },
    Location = new Location { DisplayName = "Harry's Bar" },
    Attendees = new List<Attendee>
    {
        new Attendee
        {
            EmailAddress = new EmailAddress { Address = "xero.01234@contoso.com", Name = "Orex" },
            Type = AttendeeType.Required
        }
    },
    IsOnlineMeeting = true,
    OnlineMeetingProvider = OnlineMeetingProviderType.TeamsForBusiness,
    Attachments = new List<Attachment>
    {
        new Attachment
        {
            Id = "74d20c7f34aa4a7fb74e2b30004247c5",
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = adaptiveCardJson
        }
    }
};

// 创建日历事件
await graphClient.Me.Calendar.Events.Request().AddAsync(newEvent);

3. 向邮件添加Adaptive Card附件示例

逻辑与日历事件类似,只需构建Message对象并发送:

// 构建邮件对象
var newMessage = new Message
{
    Subject = "带Adaptive Card的邮件",
    Body = new ItemBody
    {
        ContentType = BodyType.Html,
        Content = "<attachment id=\"card123\"></attachment>"
    },
    ToRecipients = new List<Recipient>
    {
        new Recipient
        {
            EmailAddress = new EmailAddress { Address = "xero.01234@contoso.com", Name = "Orex" }
        }
    },
    Attachments = new List<Attachment>
    {
        new Attachment
        {
            Id = "card123",
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = adaptiveCardJson
        }
    }
};

// 发送邮件
await graphClient.Me.SendMail(newMessage, saveToSentItems: true).Request().PostAsync();

内容的提问来源于stack exchange,提问作者Xero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:50:34