交换EWS获取房间日历

Iso*_*ric 8 c# calendar exchangewebservices

我正试图让特定房间的所有会议,也许我走错了路,但到目前为止,当我冒充房间然后得到日历视图时产生最有希望的结果 - 问题是每个日历条目主题包含用户的名称,而不是实际的会议主题.例如,它不包含包含"预算会议"的主题数据成员,而是包含"Bob Smith".

有没有更好的方法来获取特定房间的日历条目列表?这是我的代码:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("bobsled@yourdomain.onmicrosoft.com", "password");
service.AutodiscoverUrl("bobsmith@yourdomain.com", RedirectionUrlValidationCallback);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "sanfrancisco@yourdomain.onmicrosoft.com");

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(30);
const int NUM_APPTS = 5;

CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

foreach (Appointment a in appointments)
{
    Console.Write("Subject: " + a.Subject.ToString() + " ");
    Console.Write("Start: " + a.Start.ToString() + " ");
    Console.Write("End: " + a.End.ToString());
    Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)

a p*_*a p 4

如果您想尝试其他方法,您可以获取所有约会,然后按位置进行筛选:

ExchangeServices.ExchangeService exchangeService = connectToServiceWhatever(userInfo); // have working service
var folderView = new ExchangeServices.FolderView(100);   // or something like 100, idunno
folderView.Traversal = ExchangeServices.FolderTraversal.Deep;
folderView.PropertySet = new ExchangeServices.PropertySet(ExchangeServices.FolderSchema.FolderClass,
                ExchangeServices.FolderSchema.DisplayName, ExchangeServices.FolderSchema.TotalCount,
                ExchangeServices.FolderSchema.ParentFolderId);   // ... and/or whatever else you want to get - folderclass is important though. 
ExchangeServices.FindFoldersResults folders = exchangeService.FindFolders(ExchangeServices.WellKnownFolderName.MsgFolderRoot, folderView);
Run Code Online (Sandbox Code Playgroud)

现在您需要做的就是过滤文件夹类型,获取所有项目,然后过滤房间:

var appointments = folders.Where(f => f.FolderClass == "IPF.Appointment").SelectMany(f => exchangeService.FindItems(f.Id, new ExchangeServices.ItemView(folder.TotalCount < 5 ? folder.TotalCount : 5)).Where(a => a.Location == "Boardroom");  // or whatever room you want. 
Run Code Online (Sandbox Code Playgroud)

这可能无法复制粘贴,因为我现在才打出来,但我希望这足以让大家理解这个想法。您最终会做更多的工作,但希望您最终也能够检查appointment.Subject、appointment.Start|End.ToUniversalTime()等。

这是工作代码:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new WebCredentials("bobsmith@yourdomain.onmicrosoft.com", "password");
service.AutodiscoverUrl("bobsmith@yourdomain.onmicrosoft.com", RedirectionUrlValidationCallback);

var folderView = new FolderView(100);   // or something like 100, idunno
folderView.Traversal = FolderTraversal.Deep;
folderView.PropertySet = new PropertySet(FolderSchema.FolderClass,FolderSchema.DisplayName, FolderSchema.TotalCount,FolderSchema.ParentFolderId);   // ... and/or whatever else you want to get - folderclass is important though. 

FindFoldersResults folders = service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderView);
// Process each item.
foreach (Folder myFolder in folders.Folders)
{
    if (myFolder is CalendarFolder)
    {
        var calendar = (myFolder as CalendarFolder);
        // Initialize values for the start and end times, and the number of appointments to retrieve.
        DateTime startDate = DateTime.Now;
        DateTime endDate = startDate.AddDays(30);
        const int NUM_APPTS = 15;
        // Set the start and end time and number of appointments to retrieve.
        CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);
        // Limit the properties returned to the appointment's subject, start time, and end time.
        cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);
        // Retrieve a collection of appointments by using the calendar view.
        FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);
        foreach (Appointment a in appointments)
        {
            Console.Write("Subject: " + a.Subject.ToString() + " ");
            Console.Write("Start: " + a.Start.ToString() + " ");
            Console.Write("End: " + a.End.ToString());
            Console.WriteLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)