获取Outlook约会,包括使用EWS的重复约会

时间:2016-11-28 09:29:58

标签: c# ssis outlook exchangewebservices recurring

我已经实施了一个SSIS包,可以从特定的共享展望日历中获取所有任命。

我注意到,重复的那些不会被返回,因为它们是一种虚拟的。只有他们的主人才能让我有能力获得经常性的。

查看我的代码,您是否有任何建议我如何检索重复的代码?

我只是有点卡住,任何暗示都非常受欢迎!

#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);
    }
}

1 个答案:

答案 0 :(得分:0)

我自己解决了我的问题:)

我没有使用ItemView类,而是使用CalendarView类。

CalendarView也扩展了定期约会,就是这样:)