Exchange server 获取组织者';使用EWS for Exchange 2010的日历约会

Exchange server 获取组织者';使用EWS for Exchange 2010的日历约会,exchange-server,exchangewebservices,Exchange Server,Exchangewebservices,我有一个与Exchange 2010同步约会的同步应用程序,我有一些问题 UserA创建约会并将UserB添加为与会者,这意味着UserA是约会的组织者,UserB outlook日历将创建约会条目 UserA和UserB日历约会将有自己的ID(UniqueID) 例如,如果我只获得UserB日历约会的ID(UniqueID),那么这是检索UserA日历约会的方法吗?因为我认为它们应该是组织者和与会者约会之间的某种联系,我只是不知道怎么做 对于任何来找我的人-这里是如何工作的细节。我还将用源文件

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

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

  • 对于任何来找我的人-这里是如何工作的细节。我还将用源文件发布对我博客的引用

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

    这假设您对EWS是什么以及如何使用它有一些先验知识()。这也是基于博客帖子“”和帖子“”生成的

    要使其工作所需的设置:

  • 可以是“委托”或具有相应帐户“模拟”权限的用户帐户
  • 问题:exchange中的每个“约会”都有一个唯一的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 );
      }
    }
    
    [TestFixture]
    公共类BookandFindRelatedPoitmentTest
    {
    公用常量字符串ExchangeWebServiceUrl=”https://contoso.com/ews/Exchange.asmx";
    [测试]
    任命相关的公共无效测试()
    {
    ExchangeService服务=GetExchangeService();
    //模拟正在创建约会请求的用户
    service.ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.PrincipalName,“Test1”);
    约会apptRequest=CreateAppointmentRequest(服务,新与会者(“Test2@contoso.com" ) );
    //创建约会后,我们必须重新绑定约会的数据,以便设置唯一Id
    apptRequest=Appointment.Bind(服务,apptRequest.Id);
    //模拟与会者并在其日历上找到约会
    service.ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.PrincipalName,“Test2”);
    //睡眠一秒钟,让会议请求传播YMMV,因此您可能需要增加此测试的睡眠时间
    系统线程线程睡眠(1000);
    约会相关应用程序=FindRelatedAppPoint(服务、应用程序请求);
    Assert.AreNotEqual(apptRequest.Id,relatedAppt.Id);
    Assert.AreEqual(apptRequest.ICalUid,relatedAppt.ICalUid);
    }
    私有静态约会CreateAppointmentRequest(ExchangeService服务,params Attendee[]Attendes)
    {
    //创建约会。
    预约=新预约(服务)
    {
    //设置约会的属性。
    Subject=“测试预约”,
    Body=“测试Exchange服务和约会关系。”,
    开始=日期时间。现在,
    End=DateTime.Now.AddHours(1),
    Location=“你妈妈的房子”,
    };
    //加上服务员
    Array.ForEach(attendes,a=>appointment.requiredAttendes.Add(a));
    //保存约会并发送邀请
    保存(SendInvitationMode.SendToAllAndSaveCopy);
    回程预约;
    }
    /// 
    ///查找相关的约会。
    /// 
    ///服务。
    ///appt请求。
    /// 
    私有静态约会FindRelatedAppoint(ExchangeService服务、约会apptRequest)
    {
    var filter=new SearchFilter.IsEqualTo
    {
    PropertyDefinition=新扩展的PropertyDefinition
    (DefaultExtendedPropertySet.Meeting,0x03,MapPropertyType.Binary),
    Value=GetObjectedStringFromUID(apptRequest.ICalUid)//十六进制值转换为字节并进行base64编码
    };
    var view=newitemview(1){PropertySet=newpropertyset(BasePropertySet.FirstClassProperties)};
    return service.FindItems(WellKnownFolderName.Calendar,filter,view)。项[0]作为约会;
    }
    /// 
    ///获取exchange服务。
    /// 
    /// 
    私有静态ExchangeService GetExchangeService()
    {
    //您也可以使用自动发现,但在我的场景中,我已将其关闭
    返回新的ExchangeService(ExchangeVersion.Exchange2007_SP1)
    {
    凭据=新的System.Net.NetworkCredential(“dan.test”、“Password1”),
    Url=新Uri(ExchangeWebServiceUrl)
    };
    }
    /// 
    ///从uid获取对象id字符串。
    ///UID格式为十六进制字符串,GlobalObjectId显示为Base64字符串。
    /// 
    ///uid。
    /// 
    私有静态字符串GetObjectedStringFromUID(字符串id)
    {
    var buffer=新字节[id.长度/2];
    对于(int i=0;i