Exchange2007专题(三) 日历编程
现今人们可以在电子设备中输入重要的约会信息,一旦到了时间,设备会进行事件提醒,这是一个很不错的功能,MS Office就为我们提供了这样的功能。它可以向人们展示由天、周或月的活动日历。Office的数字助理提供了优秀的功能支持。要实现日历功能,搭建一台可靠稳定的日历服务器是不小的任务。所幸,Exchange为我们提供了Web Service(EWS)作为日历服务器交互的协作平台。通过EWS我们可以对日历进行二次开发。
一、创建一个日历项:
1 public static ItemIdType CreateCalendarItem(ExchangeServiceBinding binding)
2 {
3 CalendarItemType newCalendarItem = new CalendarItemType();
4 newCalendarItem.Subject = "新建一个日历项";
5 CreateItemType createItemRequest = new CreateItemType();
6
7 // 当使用CreateItem 方法创建一个日历项, 必须对 SendMeetingInviations 属性赋值 //
8 createItemRequest.SendMeetingInvitations =
9 CalendarItemCreateOrDeleteOperationType.SendToNone;
10 createItemRequest.SendMeetingInvitationsSpecified = true;
11
12 createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
13 createItemRequest.Items.Items = new CalendarItemType[1];
14 createItemRequest.Items.Items[0] = newCalendarItem;
15 // 创建一个请求 //
16 CreateItemResponseType response = binding.CreateItem(createItemRequest);
17 ItemInfoResponseMessageType responseMessage = response.ResponseMessages.Items[0] as
18 ItemInfoResponseMessageType;
19 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
20 {
21 throw new Exception("CreateCalendarItem failed with response code " +
22 responseMessage.ResponseCode.ToString());
23 }
24 return responseMessage.Items.Items[0].ItemId;
25 }
26
二、日历项的三个概念:
Recurrences
Meetings
Free/Busy 状态
Recurrences:
一个日历项去遵循不同的模式,是由一个定期序列确定的。你可以设置一个定期序列,如每周一早上9点开个例会;表示按规定时间复发事件;
当创建了一个定期序列,日历项明确已知的,每次发生的,给予指定人的日历项;
定期序列需注意定期时间和时区问题;
Meetings:
日历项可以定义一个或多个会议预约,在Exchange Web Service,这些预约都会提醒与会者;
与会者存在在一个日历项三个不同的邀请人字段中,分类收集他们的日历项目并检查IsMeeting属性判断是否是会议;
Free/Busy状态:
每个日历项会消耗用户日历的时间,随着时间槽的变化,应用到一定的时间,显示空闲或忙碌的状态,如一个会议在2~3点开,当2点半时,其他同事提起一个会议会发现你正在忙碌;
三、查看相邻日历和冲突日历
用户使用日历的习惯:
1.看看今天的日程安排;
2.看看是否有一些时间可以挤进更多的预约;
查某一区间的会议
1 public static void FunWithDateTimeKind()
2 {
3 DateTime dateTime = new DateTime(2010, 07, 29, 08, 15, 00,
4 DateTimeKind.Unspecified);
5
6 CalendarItemType calItemUnspecified = new CalendarItemType();
7 calItemUnspecified.Start = dateTime;
8 calItemUnspecified.End = calItemUnspecified.Start.AddHours(1);
9 calItemUnspecified.StartSpecified = true;
10 calItemUnspecified.EndSpecified = true;
11 SerializeToConsole("DateTimeKind.Unspecified", calItemUnspecified);
12
13 CalendarItemType calItemLocal = new CalendarItemType();
14 calItemLocal.Start = dateTime.ToLocalTime();
15 calItemLocal.End = calItemLocal.Start.AddHours(1);
16 calItemLocal.StartSpecified = calItemLocal.EndSpecified = true;
17 SerializeToConsole("DateTimeKind.Local", calItemLocal);
18
19 CalendarItemType calItemUTC = new CalendarItemType();
20 calItemUTC.Start = dateTime.ToUniversalTime();
21 calItemUTC.End = calItemUTC.Start.AddHours(1);
22 calItemUTC.StartSpecified = calItemUTC.EndSpecified = true;
23 SerializeToConsole("DateTimeKind.UTC", calItemUTC);
24
25 }
26
27
28 public static void SerializeToConsole(string header, CalendarItemType calendarItem)
29 {
30 XmlSerializer serializer = new XmlSerializer(typeof(CalendarItemType));
31
32 Console.WriteLine(header);
33 Console.WriteLine("--------------------------");
34 using (Stream outStream = Console.OpenStandardOutput())
35 {
36 serializer.Serialize(outStream, calendarItem);
37 }
38 Console.WriteLine();
39 Console.WriteLine();
40 }
41
四、创建一个会议邀请
public static ItemIdType CreateNewMeetingAndInviteAttendees(
ExchangeServiceBinding binding,
DateTime meetingStartTime,
DateTime meetingEndTime,
TimeZoneType meetingTimeZone,
EmailAddressType[] requiredAttendees,
EmailAddressType[] optionalAttendees,
EmailAddressType meetingRoom)
{
// 日历项信息
//
CalendarItemType newCalendarItem = new CalendarItemType();
newCalendarItem.Start = meetingStartTime;
newCalendarItem.StartSpecified = true;
newCalendarItem.End = meetingEndTime;
newCalendarItem.EndSpecified = true;
newCalendarItem.MeetingTimeZone = meetingTimeZone;
// 设置会议室名
//
newCalendarItem.Location = meetingRoom.Name;
newCalendarItem.Subject = "Meeting in room " + meetingRoom.Name;
// 创建一个实例并为每个EmailAddreesType增加与会者 //
newCalendarItem.RequiredAttendees = new
AttendeeType[requiredAttendees.Length];
for (int x = 0; x < requiredAttendees.Length; x++)
{
newCalendarItem.RequiredAttendees[x] = new AttendeeType();
newCalendarItem.RequiredAttendees[x].Mailbox = requiredAttendees[x];
}
newCalendarItem.OptionalAttendees = new
AttendeeType[optionalAttendees.Length];
for (int x = 0; x < optionalAttendees.Length; x++)
{
newCalendarItem.OptionalAttendees[x] = new AttendeeType();
newCalendarItem.OptionalAttendees[x].Mailbox = optionalAttendees[x];
}
newCalendarItem.Resources = new AttendeeType[1];
newCalendarItem.Resources[0] = new AttendeeType();
newCalendarItem.Resources[0].Mailbox = meetingRoom;
// 创建请求--关键步骤!
//
CreateItemType createItemRequest = new CreateItemType();
createItemRequest.SendMeetingInvitations =
CalendarItemCreateOrDeleteOperationType.SendOnlyToAll;
createItemRequest.SendMeetingInvitationsSpecified = true;
createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
createItemRequest.Items.Items = new CalendarItemType[] { newCalendarItem };
CreateItemResponseType response = binding.CreateItem(createItemRequest);
ItemInfoResponseMessageType responseMessage =
response.ResponseMessages.Items[0] as
ItemInfoResponseMessageType;
if (responseMessage.ResponseCode != ResponseCodeType.NoError)
{
throw new Exception(String.Format(
"Unable to create a new meeting\r\n{0}\r\n{1}",
responseMessage.ResponseCode,
responseMessage.MessageText));
}
return responseMessage.Items.Items[0].ItemId;
}
流程如图:
五、响应一个会议请求
1 public static ItemIdType RespondToMeetingRequest(
2 ExchangeServiceBinding binding,
3 ItemIdType meetingMessageOrCalendarItemId,
4 ResponseTypeType responseToRegister,
5 string meetingResponseBody,
6 bool requestDeliveryReceipt,
7 bool requestReadReceipt)
8 {
9 WellKnownResponseObjectType meetingResponseObject;
10 switch (responseToRegister)
11 {
12 case ResponseTypeType.Accept:
13 meetingResponseObject = new AcceptItemType();
14 break;
15 case ResponseTypeType.Tentative:
16 meetingResponseObject = new TentativelyAcceptItemType();
17 break;
18 case ResponseTypeType.Decline:
19 meetingResponseObject = new DeclineItemType();
20 break;
21 default:
22 throw new ArgumentException("ResponseTypeType value of "
23 + responseToRegister + " is not a valid meeting " +
24 "registration response.");
25 }
26
27 meetingResponseObject.ReferenceItemId = meetingMessageOrCalendarItemId;
28 meetingResponseObject.IsDeliveryReceiptRequested =
29 requestDeliveryReceipt;
30 meetingResponseObject.IsDeliveryReceiptRequestedSpecified = true;
31 meetingResponseObject.IsReadReceiptRequested =
32 requestReadReceipt;
33 meetingResponseObject.IsReadReceiptRequestedSpecified = true;
34
35 if (!String.IsNullOrEmpty(meetingResponseBody))
36 {
37 meetingResponseObject.Body = new BodyType();
38 meetingResponseObject.Body.BodyType1 = BodyTypeType.Text;
39 meetingResponseObject.Body.Value = meetingResponseBody;
40 }
41
42 CreateItemType createItemRequest = new CreateItemType();
43 createItemRequest.MessageDisposition = MessageDispositionType.SendOnly;
44 createItemRequest.MessageDispositionSpecified = true;
45 createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
46 createItemRequest.Items.Items =
47 new WellKnownResponseObjectType[] { meetingResponseObject };
48
49 CreateItemResponseType response = binding.CreateItem(createItemRequest);
50 ItemInfoResponseMessageType responseMessage =
51 response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
52
53 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
54 {
55 throw new Exception(String.Format(
56 "Unable to register for meeting\r\n{0}\r\n{1}",
57 responseMessage.ResponseCode,
58 responseMessage.MessageText));
59 }
60
61 if (responseMessage.Items.Items != null)
62 {
63 return responseMessage.Items.Items[0].ItemId;
64 }
65 else
66 {
67 return null;
68 }
69 }
70
六、取消一个会议
1 public static void RemoveACancelledMeeting(
2 ExchangeServiceBinding binding,
3 ItemIdType meetingCancellationId)
4 {
5
6 RemoveItemType removeItem = new RemoveItemType();
7 removeItem.ReferenceItemId = meetingCancellationId;
8
9 CreateItemType createItemRequest = new CreateItemType();
10 createItemRequest.MessageDisposition = MessageDispositionType.SendOnly;
11 createItemRequest.MessageDispositionSpecified = true;
12 createItemRequest.Items = new NonEmptyArrayOfAllItemsType();
13 createItemRequest.Items.Items =
14 new RemoveItemType[] { removeItem };
15
16 CreateItemResponseType response = binding.CreateItem(createItemRequest);
17 ItemInfoResponseMessageType responseMessage =
18 response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
19 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
20 {
21 throw new Exception(String.Format(
22 "Unable to remove the meeting and/or " +
23 "meeting cancellation message\r\n{0}\r\n{1}",
24 responseMessage.ResponseCode,
25 responseMessage.MessageText));
26 }
27 }
28
上述内容简单了讲述了通过代码编程实现会议、日历项的一些功能,下节我们将通过一个实例,讲述在实际项目中如何应用。