2013-02-13 42 views
4

我使用ExchangeService(ExchangeVersion.Exchange2010_SP1)如何接受和使用Exchange Web服務

我想接受並添加分類來RequiredAttendees約會添加類別到RequiredAttendees約會。要做到這一點,我需要找到這些約會。

我在EWS的理解是,對具有新的會議請求爲每個「要求與會者」創建RequiredAttendees預約的保存。

如何我可以訪問了爲'要求與會者的自動創建的約會?這些顯示在所需的與會者日曆作爲約會,以及會議請求。

我已經成功地做到粗找到主題(下面的步驟)

  1. 連接到服務器作爲組織者
  2. 創建約會
  3. 集主題
  4. 添加必需的與會者
  5. 保存預約

  6. 連接到服務器的熱曲從步驟IRED與會者4

  7. 有主題在步驟3
  8. 到預約步驟在步驟7
  9. 添加類別7
  10. 更新預約接受步驟約會
  11. 查找預約7

而且這確實有用,但有關用戶會改變主題。

我已經嘗試添加擴展屬性和價值的組織者,然後FindItems在連接作爲必需與會者的約會擴展屬性值創建的約會。這不起作用。

是否有什麼,我試圖完成一個優選的方法是什麼?

謝謝

Private Shared ReadOnly m_organiserEmailAddress As String = "[email protected]" 
Private Shared ReadOnly m_eventIdExtendedPropertyDefinition As New ExtendedPropertyDefinition(DefaultExtendedPropertySet.Meeting, "EventId", MapiPropertyType.Long) 

'--> start here 
Public Shared Function SaveToOutlookCalendar(eventCalendarItem As EventCalendarItem) As String 

    If eventCalendarItem.Id Is Nothing Then 
     'new 
     Dim newAppointment = EventCalendarItemMapper.SaveNewAppointment(eventCalendarItem) 
     'set the Id 
     eventCalendarItem.Id = newAppointment.Id.UniqueId.ToString() 
     'accept the calendar item on behalf of the Attendee 
     EventCalendarItemMapper.AcceptAppointmentAsAttendees(newAppointment) 
     Return eventCalendarItem.Id 
    Else 
     'update existing appointment 
     Return EventCalendarItemMapper.UpdateAppointment(eventCalendarItem) 
    End If 


End Function 

Private Shared Sub ConnectToServer(Optional autoUser As String = "") 

    If autoUser = "" Then 
     _service.Url = New Uri(ExchangeWebServicesUrl) 
    Else 
     _service.AutodiscoverUrl(autoUser) 
    End If 

End Sub 

Private Shared Sub ImpersonateUser(userEmail As String) 

    _service.Credentials = New NetworkCredential(ImpersonatorUsername, ImpersonatorPassword, Domain) 
    _service.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, userEmail) 

End Sub 

Private Shared Function SaveNewAppointment(eventCalendarItem As EventCalendarItem) As Appointment 

    Try 
     ConnectToServer(m_organiserEmailAddress) 
     ImpersonateUser(m_organiserEmailAddress) 

     Dim appointment As New Appointment(_service) With { 
        .Subject = eventCalendarItem.Subject} 

     'add attendees 
     For Each attendee In eventCalendarItem.Attendees 
      appointment.RequiredAttendees.Add(attendee.Email) 
     Next 

     'add categories 
     For Each category In eventCalendarItem.Categories 
      appointment.Categories.Add(Globals.GetEnumDescription(category)) 
     Next 

     'add EventId = 5059 as an extended property of the appointment 
     appointment.SetExtendedProperty(m_eventIdExtendedPropertyDefinition, 5059) 

     appointment.Save(SendInvitationsMode.SendOnlyToAll) 

     Return appointment 
    Catch 
     Throw New Exception("Can't save appointment") 
    End Try 


End Function 

Private Shared Sub AcceptAppointmentAsAttendees(appointment As Appointment) 

    For Each attendee In appointment.RequiredAttendees 
     Try 
      ConnectToServer(attendee.Address.ToString()) 
      ImpersonateUser(attendee.Address.ToString()) 

      For Each a In FindRelatedAppiontments(appointment) 
       a.Categories.Add(Globals.GetEnumDescription(CalendarItemCategory.Workshop)) 
       a.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone) 
       a.Accept(True) 
      Next 

     Catch 
      Throw 
     End Try 
    Next 
End Sub 

Private Shared Function FindRelatedAppiontments(appointment As Appointment) As List(Of Appointment) 

    Dim view As New ItemView(1000) 
    Dim foundAppointments As New List(Of Appointment) 

    view.PropertySet = 
     New PropertySet(New PropertyDefinitionBase() {m_eventIdExtendedPropertyDefinition}) 

    'Extended Property value = 5059 
    Dim searchFilter = New SearchFilter.IsEqualTo(m_eventIdExtendedPropertyDefinition, 5059) 

    For Each a In _service.FindItems(WellKnownFolderName.Calendar, searchFilter, view) 
     If a.ExtendedProperties.Count > 0 Then 
      foundAppointments.Add(appointment.Bind(_service, CType(a.Id, ItemId))) 
     End If 
    Next 

    Return foundAppointments 

End Function 
+0

爲什麼擴展屬性不起作用?他們是否從組織者複製到與會者? – BraveHeart 2013-02-14 10:12:33

+0

@BraveHeart不幸的是,如果在步驟6我連接到服務器作爲管理器,爲FindItems設置SearchFilter爲我的擴展屬性值返回Appointment。使用相同的代碼,但作爲必需參加者連接到服務器否返回約會。我已經在FindItems WellKnownFolderName.Inbox中爲會議請求嘗試了WellKnownFolderName.Calendar,並在Appointment中嘗試了這種方法。 – 2013-02-14 11:32:25

+0

說實話,我不認爲我真的走了你的意思是100%,或者你爲什麼要這麼做。 也許如果你把你的一些代碼解釋爲你如何作爲組織者或與會者連接到服務器。你有兩個應用程序嗎? 我可能會建議您使用[流式通知](http://msdn.microsoft.com/zh-cn/library/exchange/hh312849(v = exchg.140).aspx),以便您可以檢測到達與會者的日曆。 – BraveHeart 2013-02-14 12:26:35

回答

2

好一兩件事是肯定的,沒有什麼直接的EWS中。 我會誠實地說,直到現在,我沒有從內部日曆到Exchange日曆的整合工作,我的經驗是相反的,這是交換內部的東西。

反正閱讀你的代碼後,我認爲你是幾乎沒有。不過,我建議你通過使用Streaming Notifications來達到與會者的約會,這並不難! 所以,我要說的步驟應該是這樣的

  1. 創建約會
  2. 應用擴展屬性的東西(我建議,而不是使用硬編碼數GUID)如下

創建擴展屬性,並把GUID的任命,並且它不會改變,除非你做從另一個約會

private static readonly PropertyDefinitionBase AppointementIdPropertyDefinition = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.PublicStrings, "AppointmentID", MapiPropertyType.String); 
public static PropertySet PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointementIdPropertyDefinition); 


//Setting the property for the appointment 
public static void SetGuidForAppointement(Appointment appointment) 
{ 
    try 
    { 
     appointment.SetExtendedProperty((ExtendedPropertyDefinition)AppointementIdPropertyDefinition, Guid.NewGuid().ToString()); 
     appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone); 
    } 
    catch (Exception ex) 
    { 
     // logging the exception 
    } 
} 

//Getting the property for the appointment 
public static string GetGuidForAppointement(Appointment appointment) 
{ 
    var result = ""; 
    try 
    { 
     appointment.Load(PropertySet); 
     foreach (var extendedProperty in appointment.ExtendedProperties) 
     { 
      if (extendedProperty.PropertyDefinition.Name == "AppointmentID") 
      { 
       result = extendedProperty.Value.ToString(); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
    // logging the exception 
    } 
    return result; 
} 
複印件(畢竟它只是一個屬性)
  1. 捕獲與StreamingNotificationEvent的約會。在我看來,一個很好的方法是同時運行組織者和參與者,並在他們之間找到約會。 要查看示例,我已發佈對以前的問題Multiple impersonation-threads in Exchange Web Service (EWS)的回答。如果您發現我的答案有用,請投票選出這兩個帖子的答案(這裏和那裏)。

我不想讓你害怕,但是一旦你解決了你目前的問題,如果你想繼續它將變得更加複雜。我可以寫下我是如何解決會議問題的,但是我根本沒有看到它,所以如果你寫自己的話會更好。

乾杯

+0

謝謝,我將在本週末看看你的建議,我會讓你知道我是怎麼回事。我不太害怕;-) – 2013-02-15 16:15:38

+0

對不起,這是一段時間。我回到了這個項目中,併爲創建所需參加者的日曆項目設置了一個帶有事件處理程序的流通知功能。謝謝。我需要整理代碼,完成後會發布解決方案併爲您投票。 – 2013-04-03 09:54:33

相關問題