使用 Exchange 中的 EWS 删除约会和取消会议
了解如何使用 Exchange 中的 EWS 托管 API 或 EWS 删除约会和会议。
会议和约会之间的本质区别是,会议有与会者,而约会没有。 约会和会议可以是单个实例或定期系列的一部分,但由于约会不包括与会者、会议室或资源,因此不需要发送消息。 在内部,Exchange 对会议和约会都使用相同的对象。 可使用 EWS 托管 API 约会类或 EWS [CalendarItem](https://msdn.microsoft.com/library/TitleTopic ID Project Name Writer Editor Publish Preview.aspx)元素处理会议和约会。
表 1. 用于删除约会和会议的 EWS 托管 API 方法和 EWS 操作
EWS 托管 API 方法 | EWS 操作 | 功能 |
---|---|---|
Appointment.Delete |
DeleteItem |
删除约会。 |
Appointment.Delete |
CreateItem(日历项目) |
删除会议。 |
请注意,在使用 EWS 删除约会时,使用 DeleteItem 操作,但在删除会议时,则使用 CreateItem 操作。 这似乎有悖常理,但由于需要创建一个会议响应对象来向与会者发送取消会议的消息,因此采用上述操作规则。
使用 EWS 托管 API 删除约会
以下代码示例演示了如何使用 Delete 方法从日历文件夹中删除约会,以及如何使用 ExchangeService.FindItems 方法在“已删除项”文件夹中查找约会来验证该约会是否已删除。
此示例假定已对 Exchange 服务器进行了身份验证,并且已获取名为 service 的 ExchangeService 对象。 本地变量 appointmentId
是与现有约会关联的标识符。
// Instantiate an appointment object by binding to it by using the ItemId.
// As a best practice, limit the properties returned to only the ones you need.
Appointment appointment = Appointment.Bind(service, appointmentId, new PropertySet());
// Delete the appointment. Note that the item ID will change when the item is moved to the Deleted Items folder.
appointment.Delete(DeleteMode.MoveToDeletedItems);
// Verify that the appointment has been deleted by looking for a matching subject in the Deleted Items folder's first entry.
ItemView itemView = new ItemView(1);
itemView.Traversal = ItemTraversal.Shallow;
// Just retrieve the properties you need.
itemView.PropertySet = new PropertySet(ItemSchema.Id, ItemSchema.ParentFolderId, ItemSchema.Subject);
// Note that the FindItems method results in a call to EWS.
FindItemsResults<Item> deletedItems = service.FindItems(WellKnownFolderName.DeletedItems, itemView);
Item deletedItem = deletedItems.First();
Folder parentFolder = Folder.Bind(service, deletedItem.ParentFolderId, new PropertySet(FolderSchema.DisplayName));
Console.WriteLine("The appointment " + "\"" + deletedItem.Subject + "\"" + " is now in the " + parentFolder.DisplayName + " folder.");
本示例演示了一种验证约会是否已删除的简单方法,即验证“已删除项”文件夹中第一个项目的主题是否与已删除约会的主题一致。 选择用于验证约会是否已删除的方法将根据应用程序的需求而有所不同。
如你所见,删除约会非常简单,很容易上手。 请注意,创建验证步骤时,“已删除项”文件夹中约会项的 ItemId 与日历文件夹中的约会项不同。 该项会被复制和删除,而不是直接移至“已删除项”文件夹。
使用 EWS 删除约会
以下示例中的请求和响应 XML 对应于使用 EWS 托管 API 删除约会中由 EWS 托管 API 代码进行的调用。 此外,还演示了用于验证约会项是否位于“已删除项”文件夹中的请求和响应 XML。
以下示例演示了 DeleteItem 操作的请求 XML,其用于删除约会。
<?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="Pacific Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:DeleteItem DeleteType="MoveToDeletedItems" SendMeetingCancellations="SendToAllAndSaveCopy">
<m:ItemIds>
<t:ItemId Id="AAMkA" ChangeKey="DwAAA" />
</m:ItemIds>
</m:DeleteItem>
</soap:Body>
</soap:Envelope>
以下示例演示了由 DeleteItem 操作返回的响应 XML。 ItemId 和 ChangeKey 属性会缩短,以实现可读性。
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="800" MinorBuildNumber="5" Version="V2_6"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:DeleteItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:DeleteItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
</m:DeleteItemResponseMessage>
</m:ResponseMessages>
</m:DeleteItemResponse>
</s:Body>
</s:Envelope>
以下示例演示了 FindItem 操作的请求 XML,该操作用于检索“已删除项”文件夹中的第一个项,以便对该项主题与已删除约会对象的主题进行比较。
<?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="Pacific Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>IdOnly</t:BaseShape>
<t:AdditionalProperties>
<t:FieldURI FieldURI="item:ItemId" />
<t:FieldURI FieldURI="item:ParentFolderId" />
<t:FieldURI FieldURI="item:Subject" />
</t:AdditionalProperties>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="1" Offset="0" BasePoint="Beginning" />
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="deleteditems" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>
以下示例演示了验证步骤中由 FindItem 操作返回的响应 XML。
注意
ItemId 和 ChangeKey 属性会缩短,以实现可读性。
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="800" MinorBuildNumber="5" Version="V2_6"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:FindItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:FindItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:RootFolder IndexedPagingOffset="1" TotalItemsInView="10748" IncludesLastItemInRange="false">
<t:Items>
<t:CalendarItem>
<t:ItemId Id="AAMkA=" ChangeKey="DwAAA" />
<t:ParentFolderId Id="AAMkA" ChangeKey="AQAAA" />
<t:Subject>Tennis lesson</t:Subject>
</t:CalendarItem>
</t:Items>
</m:RootFolder>
</m:FindItemResponseMessage>
</m:ResponseMessages>
</m:FindItemResponse>
</s:Body>
</s:Envelope>
使用 EWS 托管 API 删除会议
在删除会议时,除了从日历文件夹中删除约会项之外,可能还需要向与会者发送会议取消通知。 取消会议有以下三种方法:
选用的方法取决于取消消息中所含内容的详细程度。 Appointment.CancelMeeting 通过以参数形式传递更新的消息,使更新取消消息变得容易。 CancelMeetingMessage 允许你在发送取消消息之前修改消息的属性,以便你可以执行请求回执等操作。
本节中的代码示例演示了删除会议和发送会议取消通知的不同方法。 这些示例假定已对 Exchange 服务器进行了身份验证,并且已获取名为 service 的 ExchangeService 对象。 本地变量 meetingId
是与现有会议关联的标识符,而目标用户是该会议的组织者。
以下代码示例演示了如何使用 Appointment.Delete 方法删除会议。
// Instantiate an appointment object for the meeting by binding to it using the ItemId.
// As a best practice, limit the properties returned to only the Appointment ID.
Appointment meeting = Appointment.Bind(service, meetingId, new PropertySet());
// Delete the meeting by using the Delete method.
meeting.Delete(DeleteMode.MoveToDeletedItems, SendCancellationsMode.SendToAllAndSaveCopy);
// Verify that the meeting has been deleted by looking for a matching subject in the Deleted Items folder's first entry.
ItemView itemView = new ItemView(1);
itemView.Traversal = ItemTraversal.Shallow;
// Just retrieve the properties you need.
itemView.PropertySet = new PropertySet(ItemSchema.Id, ItemSchema.ParentFolderId, ItemSchema.Subject);
// Note that the FindItems method results in a call to EWS.
FindItemsResults<Item> deletedItems = service.FindItems(WellKnownFolderName.DeletedItems, itemView);
Item deletedItem = deletedItems.First();
Folder parentFolder = Folder.Bind(service, deletedItem.ParentFolderId, new PropertySet(FolderSchema.DisplayName));
Console.WriteLine("The meeting " + "\"" + deletedItem.Subject + "\"" + " is now in the " + parentFolder.DisplayName + " folder.");
以下代码示例演示了如何使用 CancelMeeting 方法删除会议。
// Instantiate an appointment object by binding to it using the ItemId.
// As a best practice, limit the properties returned to only the Appointment ID.
Appointment meeting = Appointment.Bind(service, meetingId, new PropertySet());
// Delete the meeting by using the CancelMeeting method.
meeting.CancelMeeting("The outdoor meeting has been cancelled due to hailstorms.");
以下代码示例演示了如何使用 Appointment.CreateCancelMeetingMessage 方法删除会议。
// Instantiate an appointment object by binding to it using the ItemId.
// As a best practice, limit the properties returned to only the Appointment ID.
Appointment meeting = Appointment.Bind(service, meetingId, new PropertySet());
// Delete the meeting by using the CreateCancelMeetingMessage method.
CancelMeetingMessage cancelMessage = meeting.CreateCancelMeetingMessage();
cancelMessage.Body = new MessageBody("The outdoor meeting has been canceled due to hailstorms.");
cancelMessage.IsReadReceiptRequested = true;
cancelMessage.SendAndSaveCopy();
使用 EWS 删除会议
以下示例中的请求和响应 XML 对应于通过 Appointment.Delete 方法使用 EWS 托管 API 删除会议中由 EWS 托管 API 代码进行的调用。
以下示例演示了使用 CreateItem 操作向与会者发送取消消息和删除会议时的请求 XML。
<?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="Pacific Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SendAndSaveCopy">
<m:Items>
<t:CancelCalendarItem>
<t:ReferenceItemId Id="AAMkA" ChangeKey="DwAAA" />
<t:NewBodyContent BodyType="HTML">The outdoor meeting has been canceled due to hailstorms.</t:NewBodyContent>
</t:CancelCalendarItem>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>
以下示例演示了响应用于删除会议的 CreateItem 操作请求而返回的 XML。
注意
ItemId 和 ChangeKey 属性会缩短,以实现可读性。
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="800" MinorBuildNumber="5" Version="V2_6"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:CreateItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Items>
<t:CalendarItem>
<t:ItemId Id="AAMkA" ChangeKey="DwAAA" />
</t:CalendarItem>
</m:Items>
</m:CreateItemResponseMessage>
</m:ResponseMessages>
</m:CreateItemResponse>
</s:Body>
</s:Envelope>
以下示例演示了 FindItem 操作的请求 XML,该操作用于检索“已删除项”文件夹中的第一个项,以便对该项主题与已删除约会对象的主题进行比较。
<?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="Pacific Standard Time" />
</t:TimeZoneContext>
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>IdOnly</t:BaseShape>
<t:AdditionalProperties>
<t:FieldURI FieldURI="item:ItemId" />
<t:FieldURI FieldURI="item:ParentFolderId" />
<t:FieldURI FieldURI="item:Subject" />
</t:AdditionalProperties>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="1" Offset="0" BasePoint="Beginning" />
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="deleteditems" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>
以下示例演示了验证步骤中由 FindItem 操作返回的 XML。
注意
Id 和 ChangeKey 属性会缩短,以实现可读性。
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="800" MinorBuildNumber="5" Version="V2_6"
xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types"
xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:FindItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:FindItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:RootFolder IndexedPagingOffset="1" TotalItemsInView="10750" IncludesLastItemInRange="false">
<t:Items>
<t:CalendarItem>
<t:ItemId Id="AAMkA" ChangeKey="DwAAA" />
<t:ParentFolderId Id="AAMkA" ChangeKey="AQAAA" />
<t:Subject>Team building exercise</t:Subject>
</t:CalendarItem>
</t:Items>
</m:RootFolder>
</m:FindItemResponseMessage>
</m:ResponseMessages>
</m:FindItemResponse>
</s:Body>
</s:Envelope>