【问题标题】:Get the organizer's calendar appointment using EWS for Exchange 2010使用 EWS for Exchange 2010 获取组织者的日历约会
【发布时间】:2011-04-19 05:55:07
【问题描述】:

我有一个与 Exchange 2010 同步约会的同步应用程序,我有一些问题。

  1. UserA 创建约会并将 UserB 添加为与会者,这意味着 UserA 是约会的组织者,并且 UserB 的 Outlook 日历将创建约会条目。
  2. UserA 和 UserB 日历约会将有自己的 ID (UniqueID)。
  3. 例如,如果我只获得了 UserB 的日历约会的 ID (UniqueID),那么这是一种检索 UserA 的日历约会的方法吗?因为我认为它们应该是组织者和与会者预约之间的某种联系,我只是不知道如何。

【问题讨论】:

    标签: exchange-server exchangewebservices


    【解决方案1】:

    对于任何追随我的人 - 以下是有关其工作原理的详细信息。我还将发布对我博客的引用以及源文件。

    简而言之 - 约会使用 UID 属性绑定在一起。此属性也称为 CleanUniqueIdentifier。虽然此示例代码可以根据以下博客文章中引用的“错误”修复进行调整,但此源代码已完成,因为要求适用于 => 2007 SP1。

    这假设您事先了解 EWS 是什么以及如何使用它 (EWS API)。这也是基于博文“EWS: UID not always the same for orphaned instances of the same meeting”和博文“Searching a meeting with a specific UID using Exchange Web Services 2007

    为此工作所需的设置:

    1. 可以是“代表”或对各个帐户具有“模拟”权限的用户帐户。

    问题:交换中的每个“约会”都有一个唯一的 id (Appointment.Id),它是确切的实例标识符。有了这个 id,如何在日历中找到所有相关实例(重复或出席者请求)?

    下面的代码概述了如何实现这一点。

    [TestFixture]
    public class BookAndFindRelatedAppoitnmentTest
    {
      public const string ExchangeWebServiceUrl = "https://contoso.com/ews/Exchange.asmx";
    
      [Test]
      public void TestThatAppointmentsAreRelated()
      {
        ExchangeService service = GetExchangeService();
    
        //Impersonate the user who is creating the Appointment request
        service.ImpersonatedUserId = new ImpersonatedUserId( ConnectingIdType.PrincipalName, "Test1" );
        Appointment apptRequest = CreateAppointmentRequest( service, new Attendee( "Test2@contoso.com" ) );
    
        //After the appointment is created, we must rebind the data for the appointment in order to set the Unique Id
        apptRequest = Appointment.Bind( service, apptRequest.Id );
    
        //Impersonate the Attendee and locate the appointment on their calendar
        service.ImpersonatedUserId = new ImpersonatedUserId( ConnectingIdType.PrincipalName, "Test2" );
        //Sleep for a second to let the meeting request propogate YMMV so you may need to increase the sleep for this test
        System.Threading.Thread.Sleep( 1000 );
        Appointment relatedAppt = FindRelatedAppointment( service, apptRequest );
    
        Assert.AreNotEqual( apptRequest.Id, relatedAppt.Id );
        Assert.AreEqual( apptRequest.ICalUid, relatedAppt.ICalUid );
      }
    
      private static Appointment CreateAppointmentRequest( ExchangeService service, params Attendee[] attendees )
      {
        // Create the appointment.
        Appointment appointment = new Appointment( service )
        {
          // Set properties on the appointment.
          Subject = "Test Appointment",
          Body = "Testing Exchange Services and Appointment relationships.",
          Start = DateTime.Now,
          End = DateTime.Now.AddHours( 1 ),
          Location = "Your moms house",
        };
    
        //Add the attendess
        Array.ForEach( attendees, a => appointment.RequiredAttendees.Add( a ) );
    
        // Save the appointment and send out invites
        appointment.Save( SendInvitationsMode.SendToAllAndSaveCopy );
    
        return appointment;
      }
    
      /// <summary>
      /// Finds the related Appointment.
      /// </summary>
      /// <param name="service">The service.</param>
      /// <param name="apptRequest">The appt request.</param>
      /// <returns></returns>
      private static Appointment FindRelatedAppointment( ExchangeService service, Appointment apptRequest )
      {
        var filter = new SearchFilter.IsEqualTo
        {
          PropertyDefinition = new ExtendedPropertyDefinition
            ( DefaultExtendedPropertySet.Meeting, 0x03, MapiPropertyType.Binary ),
          Value = GetObjectIdStringFromUid( apptRequest.ICalUid ) //Hex value converted to byte and base64 encoded
        };
    
        var view = new ItemView( 1 ) { PropertySet = new PropertySet( BasePropertySet.FirstClassProperties ) };
    
        return service.FindItems( WellKnownFolderName.Calendar, filter, view ).Items[ 0 ] as Appointment;
      }
    
      /// <summary>
      /// Gets the exchange service.
      /// </summary>
      /// <returns></returns>
      private static ExchangeService GetExchangeService()
      {
        //You can use AutoDiscovery also but in my scenario, I have it turned off      
        return new ExchangeService( ExchangeVersion.Exchange2007_SP1 )
        {
          Credentials = new System.Net.NetworkCredential( "dan.test", "Password1" ),
          Url = new Uri( ExchangeWebServiceUrl )
        };
      }
    
      /// <summary>
      /// Gets the object id string from uid.
      /// <remarks>The UID is formatted as a hex-string and the GlobalObjectId is displayed as a Base64 string.</remarks>
      /// </summary>
      /// <param name="id">The uid.</param>
      /// <returns></returns>
      private static string GetObjectIdStringFromUid( string id )
      {
        var buffer = new byte[ id.Length / 2 ];
        for ( int i = 0; i < id.Length / 2; i++ )
        {
          var hexValue = byte.Parse( id.Substring( i * 2, 2 ), System.Globalization.NumberStyles.AllowHexSpecifier );
          buffer[ i ] = hexValue;
        }
        return Convert.ToBase64String( buffer );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 2019-06-12
      相关资源
      最近更新 更多