最近被主管交付了一個任務是研究網頁與 Outlook 行事曆整合的可行性評估
然後上網找了一下大概有幾種方式
第一種有看到是使用WebDav 伺服器,但我又沒有權限可以動 Server 何況安裝
我們公司超安全的所有權限都只有一個人在管
所以只好尋找另外的方式
第二種方式是使用 EWS (Exchange Web Services)
然後官方網站有提供API,我是用2.0 (EWS Managed API 2.0)
http://msdn.microsoft.com/en-US/exchange/aa731546
下載完之後安裝一下,然後去安裝後的資料夾裡面把dll 找出來,在專案裡加入參考就可以用了
找到的資料幾乎都 C#,但因為之前開的專案是VB,所以我就把他轉成VB了
紅字部分再請替換掉就可以了
然後這個API可以存取的當然不只行事曆
但我只被交付要研究會議邀約,所以其他就不玩了 XD
但其他的也不會很難,可以參考這邊
http://msdn.microsoft.com/en-us/library/exchange/dd633696(v=exchg.80).aspx
讀取行事曆
Imports Microsoft.Exchange.WebServices.Data
Imports System.Security.Principal
Imports System.Web.Security
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim service As New ExchangeService(ExchangeVersion.Exchange2007_SP1)
'要使用EWS之前可以參考一下此頁
'http://msdn.microsoft.com/en-us/library/exchange/dd633626(v=exchg.80).aspx
'下面的AutodiscoverUrl 照理說是要綁 EWS endpoint (就是web service位置),但如果不知道也可以用email
'請參考 http://msdn.microsoft.com/en-us/library/exchange/gg274410(v=exchg.80).aspx
service.AutodiscoverUrl("Email Address")
Dim _cal As New Microsoft.Exchange.WebServices.Data.FolderId(Microsoft.Exchange.WebServices.Data.WellKnownFolderName.Calendar, New Microsoft.Exchange.WebServices.Data.Mailbox("Email Address"))
Dim _calendarView As New Microsoft.Exchange.WebServices.Data.CalendarView(Now.Date, Now.Date.AddDays(30)) '<--- 30天內的行事曆
For Each appointmentItem As Microsoft.Exchange.WebServices.Data.Appointment In _
service.FindAppointments( _
_cal, _
_calendarView)
Response.Write(appointmentItem.Start & "-" & appointmentItem.Subject & "<br />")
Next
End Sub
End Class
設定會議邀約
Dim service As New ExchangeService(ExchangeVersion.Exchange2007_SP1)
service.AutodiscoverUrl("EMail Address")
Dim appointment As New Appointment(service)
appointment.Subject = "測試發送會議邀約"
appointment.Body = "我是邀約內容"
appointment.Start = New DateTime(2013, 2, 19, 9, 0, 0) '<--- 邀約時間
appointment.End = appointment.Start.AddHours(2)
appointment.Location = "商城大會議室"
'出席
appointment.RequiredAttendees.Add("發送邀約的 EMail Address")
appointment.RequiredAttendees.Add("發送邀約的 EMail Address")
'好像是列席
appointment.OptionalAttendees.Add("發送邀約的 EMail Address")
'如果你有很多個行事曆,可以用下面抓folder id 的方法先抓出 folder id,如參數1 (太長了,為了好閱讀所以截斷了)
appointment.Save("AAEuAPhprJ+iAAA=", SendInvitationsMode.SendToNone)
'但如果你要新增到自己的行事曆不指定的話可以這樣寫
'appointment.Save("SendInvitationsMode.SendToAllAndSaveCopy")
'其他Save方法請參考 http://msdn.microsoft.com/en-us/library/exchange/dd633661(v=exchg.80).aspx
修改會議內容
Dim service As New ExchangeService(ExchangeVersion.Exchange2007_SP1, TimeZoneInfo.Utc )
service.AutodiscoverUrl("EMail Address")
'ItemId 可以到上面讀取行事曆的地方用 appointmentItem.Id.UniqueId 來查詢,因為Id太長了所以下面是截斷的
Dim Appointment As Appointment = Appointment.Bind(service, New ItemId("AAMkAGRmOGRhNGE.........="))
'下面這行不加的話會出現錯誤訊息
Appointment.StartTimeZone = System.TimeZoneInfo.FindSystemTimeZoneById("Taipei Standard Time")
Appointment.Subject = "測試會議修改"
Appointment.Start = New DateTime(2013, 2, 19, 10, 0, 0)
Appointment.End = Appointment.Start.AddHours(1) '<--- 會議時間一個小時
'可以再加發給其他與會者
'Appointment.RequiredAttendees.Add("發送邀約的 EMail Address")
Appointment.Update(ConflictResolutionMode.AlwaysOverwrite)
刪除行事曆
Dim service As New ExchangeService(ExchangeVersion.Exchange2007_SP1, TimeZoneInfo.Utc)
service.AutodiscoverUrl("EMail Address")
'ItemId 可以到上面讀取行事曆的地方用 appointmentItem.Id.UniqueId 來查詢,因為Id太長了所以下面是截斷的
Dim Appointment As Appointment = Appointment.Bind(service, New ItemId("AAMkAGRmOGRhNGE.........="))
Appointment.Delete(DeleteMode.MoveToDeletedItems)
抓取公用資料夾的forder id
Dim rootfolder As Folder = Folder.Bind(service, WellKnownFolderName.PublicFoldersRoot)
Response.Write("The " + rootfolder.DisplayName + " has " + rootfolder.ChildFolderCount.ToString() + " child folders.")
rootfolder.Load()
For Each folder In rootfolder.FindFolders(New FolderView(100))
Response.Write(ChrW(10) + "Name: " + folder.DisplayName + ChrW(10) + " Id: " + folder.Id.ToString() & "<br>")
Next
抓取30天內的會議 UniqueId
Dim _cal As New Microsoft.Exchange.WebServices.Data.FolderId(Microsoft.Exchange.WebServices.Data.WellKnownFolderName.Calendar, New Microsoft.Exchange.WebServices.Data.Mailbox("EMail Address"))
Dim _calendarView As New Microsoft.Exchange.WebServices.Data.CalendarView(Now.Date, Now.Date.AddDays(30))
For Each appointmentItem As Microsoft.Exchange.WebServices.Data.Appointment In _
service.FindAppointments( _
_cal, _
_calendarView)
Response.Write(appointmentItem.Start & "-<font color=red>" & appointmentItem.Id.UniqueId & "</font>-" & appointmentItem.Subject & "<br />")
Next
相關文件 : http://msdn.microsoft.com/en-us/library/exchange/dd633702(v=exchg.80).aspx