在 Exchange 中使用 EWS 删除定期系列中的约会

了解如何使用 Exchange 中的 EWS 托管 API 或 EWS 删除定期系列中的约会。

可以使用 EWS 托管 API 或 EWS 删除一系列约会或会议,或者仅删除系列中的一个实例。 用于删除整个序列的过程实质上与用于删除单个事件的过程相同。 使用用于 删除单个实例约会或会议的相同 EWS 托管 API 方法或 EWS 操作。 区别在于方法或操作中包含的项 ID。 让我们首先看看这两种方案如何相同。

若要删除定期序列或定期系列中的单个事件,需要找到要删除的匹配项或系列,然后调用相应的方法或操作将其删除。 虽然只需删除任何类型的约会,但我们建议使任何与会者或组织者保持最新状态,并取消用户组织的会议,并拒绝用户未组织的会议。

那么,这些方案有何不同呢? 这与用于调用 EWS 托管 API 的方法 () 或 EWS) 的操作请求 (中包含的项 ID 的 Appointment 对象有关。 若要删除整个系列,需要定期主控服务器的 Appointment 对象或项 ID。 若要删除单个匹配项,需要该匹配项的 Appointment 对象或项 ID。

使用 EWS 托管 API 删除定期约会

此示例假定已对 Exchange 服务器进行了身份验证,并且已获取名为 serviceExchangeService 对象。 recurrenceingItem 参数是定期主控服务器或单个事件的 Appointment 对象。 deleteEntireSeries 参数指示是否删除 recurringItem 所属的整个系列。

public static bool DeleteRecurringItem(ExchangeService service, Appointment recurringItem, bool deleteEntireSeries)
{
    Appointment appointmentToDelete = null;
    // If the item is a single appointment, fail.
    if (recurringItem.AppointmentType == AppointmentType.Single)
    {
        Console.WriteLine("ERROR: The item to delete is not part of a recurring series.");
        return false;
    }
    // Check the Appointment that was passed. Is it
    // an occurrence or the recurring master?
    if (recurringItem.AppointmentType == AppointmentType.RecurringMaster)
    {
        if (!deleteEntireSeries)
        {
            // The item is the recurring master, so deleting it will delete
            // the entire series. The caller indicated that the entire series
            // should not be deleted, so fail.
            Console.WriteLine("ERROR: The item to delete is the recurring master of the series. Deleting it will delete the entire series.");
            return false;
        }
        else
        {
            appointmentToDelete = recurringItem;
        }
    }
    else
    {
        if (deleteEntireSeries)
        {
            // The item passed is not the recurring master, but the caller
            // wants to delete the entire series. Bind to the recurring
            // master to delete it.
            try
            {
                appointmentToDelete = Appointment.BindToRecurringMaster(service, recurringItem.Id);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
                return false;
            }
        }
        else
        {
            // The item passed is not the recurring master, but the caller
            // only wants to delete the occurrence, so just
            // delete the passed item.
            appointmentToDelete = recurringItem;
        }
    }
    if (appointmentToDelete != null)
    {
        // Remove the item, depending on the scenario. 
        if (appointmentToDelete.IsMeeting)
        {
            CalendarActionResults results;
            // If it's a meeting and the user is the organizer, cancel it.
            // Determine this by testing the AppointmentState bitmask for 
            // the presence of the second bit. This bit indicates that the appointment
            // was received, which means that someone sent it to the user. Therefore,
            // they're not the organizer.
            int isReceived = 2;
            if ((appointmentToDelete.AppointmentState & isReceived) == 0)
            {
                results = appointmentToDelete.CancelMeeting("Cancelling this meeting.");
                return true;
            }
            // If it's a meeting and the user is not the organizer, decline it.
            else
            {
                results = appointmentToDelete.Decline(true);
                return true;
            }
        }
        else
        {
            // The item isn't a meeting, so just delete it.
            appointmentToDelete.Delete(DeleteMode.MoveToDeletedItems);
            return true;
        }
    }
    return false;
}

若要使用此示例,需要 绑定到事件或定期主控形状,并将生成的 Appointment 对象传递给 方法。 请记住,如果使用 CalendarView 类访问约会,则生成的项目都是单个事件。 相反,如果使用 ItemView 类,则生成的项都是重复的主控形状。

使用 EWS 删除定期约会

使用 EWS 删除定期系列与 删除单实例会议相同。 事实上,SOAP 请求采用相同的格式。 同样,键是请求中使用的项 ID。 如果项 ID 对应于定期主控形状,则将删除整个系列。 如果项 ID 对应于单个匹配项,则只会删除该匹配项。

注意

在下面的代码示例中,为了提高可读性, 将缩短 ItemIdChangeKeyRecurringMasterId 属性。

此示例使用 CreateItem 操作CancelCalendarItem 元素取消用户是组织者的会议。 ReferenceItemId 元素的值指示要取消的项,可以是定期主控形状的项 ID,也可以是单个实例的项 ID。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Eastern Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendAndSaveCopy">
      <m:Items>
        <t:CancelCalendarItem>
          <t:ReferenceItemId Id="AAMkADA5..." ChangeKey="DwAAABYA..." />
          <t:NewBodyContent BodyType="HTML">Cancelling this meeting.</t:NewBodyContent>
        </t:CancelCalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

此示例使用具有 DeclineItem 元素的 CreateItem 操作来拒绝用户不是组织者的会议。 与上一个示例一样, ReferenceItemId 元素的值指示要拒绝的项目,可以是重复主控形状的项目 ID,也可以是单个实例的项目 ID。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Eastern Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:CreateItem MessageDisposition="SendAndSaveCopy">
      <m:Items>
        <t:DeclineItem>
          <t:ReferenceItemId Id="AAMkADA6..." ChangeKey="DwAAABYA..." />
        </t:DeclineItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

此示例使用 DeleteItem 操作 删除没有与会者的约会的单个匹配项。 要删除的匹配项由 OccurrenceItemId 元素指定,该元素是从定期主控形状的项 ID 和匹配项的索引构造的。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2007_SP1" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Eastern Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:DeleteItem DeleteType="MoveToDeletedItems" SendMeetingCancellations="SendToAllAndSaveCopy">
      <m:ItemIds>
        <t:OccurrenceItemId RecurringMasterId="AAMkADA8..." InstanceIndex="3" />
      </m:ItemIds>
    </m:DeleteItem>
  </soap:Body>
</soap:Envelope>

请注意,通过将 OccurrenceItemId 元素替换为包含匹配项 ID 的 ItemId 元素,可以获得相同的结果,如下所示。

<m:ItemIds>
  <t:ItemId Id="AAMkADA7..." ChangeKey="DwAAABYA..." />
</m:ItemIds>

另请参阅