简体   繁体   English

使用EWS获取Outlook约会,包括定期约会

[英]Get Outlook Appointments including recurring ones using EWS

I have implemented an SSIS Package getting all appoinments from a particular shared outlook calendar. 我已经实现了一个SSIS包,可以从特定的共享Outlook日历中获取所有订单。

What I noticed is that the recurring ones are NOT returned because they are a kind of virtual. 我注意到的是,重复发生的不返回,因为它们是一种虚拟的。 Only the Master of them will give me the ability to get also the recurring ones. 只有他们的主人会给我能力,让我也能得到那些经常出现的人。

Looking at my code, do you have any suggestions how I can retrieve also the recurring ones? 查看我的代码,您对我如何也可以检索重复代码有什么建议?

I'm just a little stuck and any hint is highly appreciated! 我只是有点卡住,任何提示都非常感谢!

#region Namespaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.Exchange.WebServices.Data;
#endregion

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    string sharedCalendarID;

    static FolderId FindPublicFolder(ExchangeService myService, FolderId baseFolderId, string folderName)
    {

        // search using paging.
        FolderView folderView = new FolderView(10, 0);
        folderView.OffsetBasePoint = OffsetBasePoint.Beginning;
        // need only DisplayName and Id of every folder
        // reduce the property set to the properties
        folderView.PropertySet = new PropertySet(FolderSchema.DisplayName, FolderSchema.Id);

        FindFoldersResults folderResults;
        do
        {
            folderResults = myService.FindFolders(baseFolderId, folderView);

            foreach (Folder folder in folderResults)
                if (String.Compare(folder.DisplayName, folderName, StringComparison.OrdinalIgnoreCase) == 0)
                    return folder.Id;

            if (folderResults.NextPageOffset.HasValue)
                // go to the next page
                folderView.Offset = folderResults.NextPageOffset.Value;
        }
        while (folderResults.MoreAvailable);

        return null;
    }

    public override void CreateNewOutputRows()
    {
        // IMPORTANT: ExchangeService is NOT thread safe, so one should create an instance of
        // ExchangeService whenever one needs it.
        ExchangeService myService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

        myService.Credentials = new NetworkCredential("username", "password");   
        myService.Url = new Uri("https://......Exchange.asmx");
        // next line is very practical during development phase or for debugging
        myService.TraceEnabled = true;

        Folder myPublicFoldersRoot = Folder.Bind(myService, WellKnownFolderName.PublicFoldersRoot);
        string myPublicFolderPath = @"CHI\WIED PFL Agenda";
        string[] folderPath = myPublicFolderPath.Split('\\');
        FolderId fId = myPublicFoldersRoot.Id;
        foreach (string subFolderName in folderPath)
        {
            fId = FindPublicFolder(myService, fId, subFolderName);
            if (fId == null)
            {
                // No Calendar found
                return;
            }
        }

        // verify 
        Folder folderFound = Folder.Bind(myService, fId);
        if (String.Compare(folderFound.FolderClass, "IPF.Appointment", StringComparison.Ordinal) == 0)
        {
            sharedCalendarID = fId.ToString(); ;
        }
        else
            return;

        CalendarFolder myPublicFolder = CalendarFolder.Bind(myService,
            //WellKnownFolderName.Calendar,
            fId,
            PropertySet.FirstClassProperties);

        // search using paging. page size 10
        ItemView viewCalendar = new ItemView(10);
        // include all properties which we need in the view
        // comment the next line then ALL properties will be read from the server. 
        //viewCalendar.PropertySet = new PropertySet(ItemSchema.Subject, ItemSchema.Id);
        viewCalendar.Offset = 0;
        viewCalendar.OffsetBasePoint = OffsetBasePoint.Beginning;
        viewCalendar.OrderBy.Add(ContactSchema.DateTimeCreated, SortDirection.Descending);
        FindItemsResults<Item> findResultsCalendar;
        do
        {
            //SearchFilter searchFilter = new SearchFilter.IsGreaterThan(AppointmentSchema.Start, DateTime.Today);
            var searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
            new SearchFilter.IsEqualTo(ItemSchema.ItemClass, "IPM.Appointment"),
            new SearchFilter.IsGreaterThanOrEqualTo(AppointmentSchema.Start, DateTime.Now),
            new SearchFilter.IsLessThan(AppointmentSchema.Start, DateTime.Now.AddDays(4)));

            findResultsCalendar = myPublicFolder.FindItems(searchFilter, viewCalendar);

            //get additional properties for each item returned by view, do this as view cannot return a lot of useful stuff like attendees
            ServiceResponseCollection<ServiceResponse> addProperties =
                        myService.LoadPropertiesForItems(from Item item in findResultsCalendar select item,
                        new PropertySet(
                                BasePropertySet.IdOnly,
                                AppointmentSchema.Body,
                                AppointmentSchema.Subject,
                                AppointmentSchema.Start,
                                AppointmentSchema.End,
                                AppointmentSchema.IsRecurring,
                                AppointmentSchema.AppointmentType
                                ));

            List<Appointment> additionalProperties = new List<Appointment>(addProperties.Count);

            if (addProperties != null)
            {
                foreach (ServiceResponse currentResponce in addProperties)
                {
                    additionalProperties.Add(((Appointment)((GetItemResponse)currentResponce).Item));
                }
            }

            foreach (Item item in findResultsCalendar)
            {
                Appointment appt = item as Appointment;

                if (item is Appointment || appt.AppointmentType == AppointmentType.RecurringMaster)
                {
                    Appointment currentAppointmentAddProps = null;
                    currentAppointmentAddProps = additionalProperties.Find(
                        delegate(Appointment arg)
                        {
                            return arg.Id == item.Id;
                        }
                    );

                    //convert to int wether the Appointment is recurring or not
                    int isRecurring = currentAppointmentAddProps.IsRecurring ? 1 : 0;

                    Appointment appoint = item as Appointment;
                    OutputRecordSetBuffer.AddRow();
                    OutputRecordSetBuffer.ActualEndDate = currentAppointmentAddProps.End;
                    OutputRecordSetBuffer.ActualStartDate = currentAppointmentAddProps.Start;
                    OutputRecordSetBuffer.Subject = appoint.Subject;
                    OutputRecordSetBuffer.EntryID = Guid.NewGuid();
                    //OutputRecordSetBuffer.Detail = appoint.Body.ToString();
                    //OutputRecordSetBuffer.CalendarID = fId.ToString();
                    //OutputRecordSetBuffer.AppointmentID = appoint.Id.ToString();
                    OutputRecordSetBuffer.CalendarID = sharedCalendarID;
                    OutputRecordSetBuffer.AppointmentID = Guid.NewGuid();
                    OutputRecordSetBuffer.IsRecurring = isRecurring;

                }
            }

            if (findResultsCalendar.NextPageOffset.HasValue)
                // go to the next page
                viewCalendar.Offset = findResultsCalendar.NextPageOffset.Value;
        }
        while (findResultsCalendar.MoreAvailable);
    }
}

I have solved my issue on my own :) 我已经自行解决了问题:)

Instead of using the ItemView class, I used the CalendarView class. 我没有使用ItemView类,而是使用了CalendarView类。

CalendarView expands also the recurring appointments, thats it :) CalendarView还会扩展定期约会,就这样:)

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

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