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

使用Microsoft Graph SDK PATCH操作无法移除活动/在线会议参会者的问题

解决Graph SDK应用权限下PATCH移除参会者无效的问题

一、Calendar Event(日历事件)的参会者移除

直接替换Attendees列表无效的核心原因是:Graph API对集合类型的更新要求严格,需确保每个Attendee对象包含完整必要属性,或使用JSON Patch显式执行移除操作。

方法1:替换完整参会者列表(含必要属性)

构造更新的Attendee对象时,必须显式指定ODataType和Type属性,确保Graph API能正确识别要保留的参会者:

var client = GetAppGraphServiceClient(config);
if (client == null) {
    Console.WriteLine("Init Graph Client Error...");
    return;
}

string userId = "user-id";
string eventId = "event-id";
// 获取原事件,确保加载Attendees完整属性
var theEvent = await client.Users[userId].Events[eventId].GetAsync(req =>
{
    req.QueryParameters.Select = new string[] { "Attendees" };
});

// 过滤并构造完整的Attendee对象
var updatedAttendees = theEvent.Attendees
    .Where(attendee => attendee.EmailAddress.Address != "OneExisted@mytenant.onmicrosoft.com")
    .Select(attendee => new Attendee
    {
        ODataType = "#microsoft.graph.attendee", // 必须指定ODataType
        EmailAddress = attendee.EmailAddress,
        Type = attendee.Type // 必须保留参会者类型(Required/Optional/Resource)
    })
    .ToList();

var updatedEvent = new Event
{
    Attendees = updatedAttendees
};

// 执行PATCH更新
var newEvent = await client.Users[userId].Events[eventId].PatchAsync(updatedEvent);

方法2:使用JSON Patch显式移除参会者

若仅需移除特定参会者,无需替换整个列表,可使用JsonPatchDocument构造移除操作:

var client = GetAppGraphServiceClient(config);
if (client == null) {
    Console.WriteLine("Init Graph Client Error...");
    return;
}

string userId = "user-id";
string eventId = "event-id";
var theEvent = await client.Users[userId].Events[eventId].GetAsync(req =>
{
    req.QueryParameters.Select = new string[] { "Attendees" };
});

// 定位要移除的参会者
var attendeeToRemove = theEvent.Attendees.FirstOrDefault(a => a.EmailAddress.Address == "OneExisted@mytenant.onmicrosoft.com");
if (attendeeToRemove != null)
{
    var patchDoc = new JsonPatchDocument<Event>();
    // 通过索引移除参会者(需确保索引对应)
    int index = theEvent.Attendees.IndexOf(attendeeToRemove);
    patchDoc.Remove(e => e.Attendees[index]);

    // 执行PATCH更新
    var newEvent = await client.Users[userId].Events[eventId].PatchAsync(patchDoc);
}

二、OnlineMeeting(在线会议)的参会者移除

注意:OnlineMeeting的参会者不能通过直接PATCH Participants属性移除,必须使用专门的RemoveParticipants API。

正确移除参会者的代码示例

var client = GetAppGraphServiceClient(config);
if (client == null) {
    Console.WriteLine("Init Graph Client Error...");
    return;
}

string userId = "user-id";
string onlineMeetingId = "meeting-id";

// 构造要移除的参会者列表
var participantsToRemove = new List<MeetingParticipantInfo>
{
    new MeetingParticipantInfo
    {
        Identity = new IdentitySet
        {
            User = new Identity
            {
                Id = "要移除的用户ID",
                DisplayName = "用户名"
            }
        }
    }
};

// 调用专门接口移除参会者
await client.Users[userId].OnlineMeetings[onlineMeetingId].RemoveParticipants.PostAsync(
    new RemoveParticipantsPostRequestBody
    {
        Participants = participantsToRemove
    });

// 验证移除结果
var updatedMeeting = await client.Users[userId].OnlineMeetings[onlineMeetingId].GetAsync(req =>
{
    req.QueryParameters.Expand = new string[] { "Participants" };
});

若需移除所有参会者,可一次性传入所有目标参会者列表,或循环调用该接口。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 00:30:15