使用EWS for Exchange 2010获取组织者的日历约会 [英] Get the organizer's calendar appointment using EWS for Exchange 2010
问题描述
我有一个与Exchange 2010进行同步约会的同步应用程序,并且我有一些疑问.
I have an synchronization application with sync appointments with Exchange 2010, and i have some questions.
- UserA创建一个约会并将UserB添加为与会者,这意味着UserA是约会的组织者,并且UserB前景日历将创建一个约会条目.
- UserA和UserB日历约会将具有其自己的ID(UniqueID).
- 例如,如果我仅获得UserB日历约会的ID(UniqueID),那是检索UserA日历约会的一种方法吗?因为我认为它们应该是组织者与与会者约会之间的某种联系,所以我只是不知道该怎么做.
推荐答案
对于跟随我的任何人-以下是有关其工作原理的详细信息.我还将使用源文件发布对博客的引用.
For anyone coming after me - here is the details on how this works. I'll also post reference to my blog with source files.
简而言之-约会使用UID属性绑定在一起.此属性也称为CleanUniqueIdentifier.虽然可以根据下面博客文章中引用的错误"修复程序来调整此示例代码,但完成此源代码是因为要求是与=> 2007 SP1一起使用.
In short - appointments are tied together using the UID property. This property is also termed as the CleanUniqueIdentifier. While this example code could be adjusted based on a "bug" fix referenced in the blog post below, this source code is done because the requirements are to work with => 2007 SP1.
This assumes you have some prior knowledge of what the EWS is and how to use it (EWS API). This also is building off of blog post "EWS: UID not always the same for orphaned instances of the same meeting" and post "Searching a meeting with a specific UID using Exchange Web Services 2007"
要使其正常运行,必须进行以下设置:
Required setup for this to work:
- 各个帐户可以是代理"或具有模拟"特权的用户帐户.
问题:交换中的每个约会"都有一个唯一的ID(Appointment.Id),它是确切的实例标识符.有了这个ID,如何在日历中找到所有相关实例(重复请求或与会者请求)?
Problem: Each "appointment" in exchange has a unique id (Appointment.Id) that is the exact instance identifier. Having this id, how can one find all related instances (recurring or attendee requests) in a calendar?
下面的代码概述了如何实现此目的.
The code below outlines how this can be accomplished.
[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 );
}
}
这篇关于使用EWS for Exchange 2010获取组织者的日历约会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!