ahl*_*lun 9 exchange-server exchangewebservices
我有一个同步应用程序与Exchange 2010同步约会,我有一些问题.
Dan*_*Dan 18
对于任何追随我的人 - 这里有关于它是如何工作的细节.我还将使用源文件发布我的博客参考.
简而言之 - 约会使用UID属性绑定在一起.此属性也称为CleanUniqueIdentifier.虽然此示例代码可以根据下面博客文章中引用的"错误"修复进行调整,但此源代码已完成,因为要求适用于=> 2007 SP1.
这假设您已经了解了EWS是什么以及如何使用它(EWS API).这也是基于博客文章" EWS:同一会议的孤立实例的UID并不总是相同 ",并发布" 使用Exchange Web Services 2007搜索具有特定UID的会议 "
此工作所需的设置:
问题:交换中的每个"约会"都有一个唯一的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 );
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10011 次 |
| 最近记录: |