繁体   English   中英

EWS托管的Api-仅检索一次日历项

[英]EWS Managed Api - Retrieve calendar items only once

我有一个Windows服务,可以从Exchange读取日历项目,并针对每个用户将其存储在应用程序的数据库中。 现在,我想知道标记存储在Exchange上的项以使其在下一次运行时无法检索的最佳方法是什么?

我尝试将扩展属性添加到日历项并设置其值,然后每次都基于其值进行搜索:

DateTime startDate = DateTime.Now;
DateTime endDate = startDate.AddDays(60);
const int NUM_APPTS = 60;    
// Set the start and end time and number of appointments to retrieve.
CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);

Guid myPropertyId = new Guid("{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}");
ExtendedPropertyDefinition myExtendedProperty = new ExtendedPropertyDefinition(myPropertyId, "SyncFlag", MapiPropertyType.Boolean);

// Limit the properties returned to the appointment's subject, start time, end time and the extended property.
 cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Location, myExtendedProperty);

// Retrieve a collection of appointments by using the calendar view.
List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
searchFilterCollection.Add(new SearchFilter.IsEqualTo(AppointmentSchema.Start, DateTime.Now));
searchFilterCollection.Add(new SearchFilter.IsEqualTo(AppointmentSchema.End, DateTime.Now.AddDays(60)));

searchFilterCollection.Add(new SearchFilter.IsNotEqualTo(myExtendedProperty, true)); //Do not fetch already marked calendar items

SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);

FindItemsResults<Item> appointments = service.FindItems(WellKnownFolderName.Calendar, searchFilter, cView);

使用此代码搜索日历项将引发以下异常:

可能未为CalendarView指定限制和排序顺序。

而且我不确定这是否也是最好的方法。 任何想法?

谢谢

一种方法是使用SyncFolderItems方法。 它将返回自上次调用以来已更改的项目。 您要做的就是在每个调用之间保存一个同步“ cookie”。

您还应该过滤掉自上次通话以来创建的约会项目。

下面的代码片段应该可以解决问题。

static private void GetChangedItems()
{
    // Path to cookie
    string syncStateFileName = @"c:\temp\ewssyncstate.txt";

    try
    {
        var service = GetServiceAP();
        bool moreChangesAvailable = false;

        do
        {
            string syncState = null;


            if (File.Exists(syncStateFileName))
            {
                // Read cookie
                syncState = File.ReadAllText(syncStateFileName);
            }

            ChangeCollection<ItemChange> icc = service.SyncFolderItems(new FolderId(WellKnownFolderName.Calendar), PropertySet.FirstClassProperties, null, 10, SyncFolderItemsScope.NormalItems, syncState);

            if (icc.Count == 0)
            {
                Console.WriteLine("There are no item changes to synchronize.");
            }
            else
            {
                syncState = icc.SyncState;

                // Save cookie
                File.WriteAllText(syncStateFileName, syncState);

                moreChangesAvailable = icc.MoreChangesAvailable;

                // Only get appointments that were created since last call
                var createdItems = icc
                    .Where(i => i.ChangeType == ChangeType.Create)
                    .Select(i => i.Item as Appointment)
                    ;

                if (createdItems.Any())
                {
                    service.LoadPropertiesForItems(createdItems, PropertySet.FirstClassProperties);

                    foreach (Appointment appointment in createdItems)
                    {
                        Console.WriteLine(appointment.Subject);
                    }
                }
            }
        }
        while (moreChangesAvailable);

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM