2

我有一个外部文件,它是一个 csharp 文件。该代码搜索 Outlook 日历中的公用文件夹,并根据根文件夹名称和日历名称返回日历数据。该代码包括例如:

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

/// <summary>
/// This is the class to which to add your code.  Do not change the name, attributes, or parent
/// of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    /// <summary>
    /// This method is called once, before rows begin to be processed in the data flow.
    ///
    /// You can remove this method if you don't need to do anything here.
    /// </summary>
    public override void PreExecute()
    {
        base.PreExecute();
        /*
         * Add your code here
         */
    }

    /// <summary>
    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don't need to do anything here.
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();
        /*
         * Add your code here
         */
    }
byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

    public override void CreateNewOutputRows()
    {
        bool pbCancel = false;
        Uri OutlookWebAccessUri = new Uri(@"https://outlook.office365.com/EWS/Exchange.asmx");

        try
        {
            #region create service binding

            // Create the service binding.
            ExchangeService esb = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            esb.Credentials = new WebCredentials("username", "password");// exchangeAccessAccount;           
            esb.Url = OutlookWebAccessUri;

            #endregion

            #region create CalendarView
            // this week --SET YOUR VIEW ON WHAT TIMELINE YOU WANT TO RETRIEVE YOUR CALENDAR DATA --
            //if no calendar data - start here
            DateTime startDate = new DateTime(Variables.StartYear, Variables.StartMonth, 1);

            DateTime endDate = new DateTime(Variables.StartYear, Variables.StartMonth, DateTime.DaysInMonth(Variables.StartYear, Variables.StartMonth));


                //there is no calendar data, get all data
                startDate = new DateTime(Variables.StartYear, Variables.StartMonth, 1);

                endDate = new DateTime(Variables.StartYear, Variables.StartMonth, DateTime.DaysInMonth(Variables.StartYear, Variables.StartMonth));


            //find the root folder
            Folder rootfolder = Folder.Bind(esb, WellKnownFolderName.PublicFoldersRoot, new PropertySet());
            FolderView view = new FolderView(100);

            SearchFilter search = new SearchFilter.ContainsSubstring(FolderSchema.DisplayName, Variables.RootFolder); //Enter your rootfolder here that you are looking for in the public folder domain

            FindFoldersResults myFolderResults = rootfolder.FindFolders(search, view);

            foreach (Folder myFolder in myFolderResults.Folders)
            {
                foreach (CalendarFolder PublicItem in myFolder.FindFolders(view))
                {

                    string myname = PublicItem.DisplayName;

                    if (myname == Variables.CalendarName) //Enter your calendar name here within the sub folder
                    {

                        CalendarFolder PublicCalendar = CalendarFolder.Bind(esb, PublicItem.Id, new PropertySet());

                        CalendarView cPublicView = new CalendarView(startDate, endDate, 512);
                        cPublicView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Id);
                        FindItemsResults<Appointment> findPublicItemResponse = PublicCalendar.FindAppointments(cPublicView);

                        if (findPublicItemResponse == null)
                        {
                            return;
                        }

                        //get additional properties for each item returned by view, do this as view cannot return a lot of useful stuff like attendees
                        ServiceResponseCollection<ServiceResponse> addPublicProperties =
                                    esb.LoadPropertiesForItems(from Item item in findPublicItemResponse select item,
                                    new PropertySet(
                                            BasePropertySet.IdOnly,
                                            AppointmentSchema.Resources,
                                            AppointmentSchema.RequiredAttendees,
                                            AppointmentSchema.OptionalAttendees,
                                            AppointmentSchema.Subject,
                                            AppointmentSchema.Start,
                                            AppointmentSchema.End,
                                            AppointmentSchema.IsCancelled,
                                            AppointmentSchema.Location,
                                            AppointmentSchema.Body,
                                            AppointmentSchema.Categories
                                            ));


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

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

                        Appointment currentAppointmentAddProps = null;

                        foreach (Appointment currentAppointment in findPublicItemResponse)
                        {
                            #region find additional properties for current Appointment

                            currentAppointmentAddProps = additionalProperties.Find(delegate (Appointment arg)
                            { return arg.Id == currentAppointment.Id; });

                            #endregion

                            OutputRecordSetBuffer.AddRow();

                            if (currentAppointmentAddProps.RequiredAttendees.Count > 0)
                            {
                                foreach (var attendee in currentAppointmentAddProps.RequiredAttendees) //what to do if no attendee?
                                {

                                    OutputRecordSetBuffer.Attendee = attendee.Name;
                                    OutputRecordSetBuffer.ActualStartDate = currentAppointmentAddProps.Start;
                                    OutputRecordSetBuffer.ActualEndDate = currentAppointmentAddProps.End;
                                    OutputRecordSetBuffer.Subject = currentAppointment.Subject;
                                    OutputRecordSetBuffer.Location = currentAppointment.Location;

                                    OutputRecordSetBuffer.Detail.AddBlobData(GetBytes(currentAppointment.Body.Text));
                                    OutputRecordSetBuffer.DetailType = currentAppointment.Body.BodyType.ToString();
                                    OutputRecordSetBuffer.CalendarName = Variables.CalendarName;
                                    //Currently no way to return category color

                                }
                            }
                            else
                            {
                                OutputRecordSetBuffer.ActualStartDate = currentAppointmentAddProps.Start;
                                OutputRecordSetBuffer.ActualEndDate = currentAppointmentAddProps.End;
                                OutputRecordSetBuffer.Subject = currentAppointment.Subject;
                                OutputRecordSetBuffer.Location = currentAppointment.Location;

                                if (currentAppointment.Body.Text != null)
                                {
                                    OutputRecordSetBuffer.Detail.AddBlobData(GetBytes(currentAppointment.Body.Text));
                                }


                                OutputRecordSetBuffer.DetailType = currentAppointment.Body.BodyType.ToString();
                                OutputRecordSetBuffer.CalendarName = Variables.CalendarName;
                            }


                        }
                    }

                }
            }





            #endregion





        }
        catch (Exception e)
        {

            ComponentMetaData.FireError(1, "Get empty calendar data for insert has error", e.Message, "", 0, out pbCancel);

        }
    }
}

由于列表中的(我认为)类型,我的 biml xml 被破坏。ServiceResponseCollection 和列表。Biml 将标签 <> 作为 biml 的一部分读取,然后不将文件的其余部分读取为 C#。如何引用 C# 文件,因为我想保持整洁,不必将大量代码发布到 Scriptprojects 中,而是将其作为单独的文件保存,如下所示。

 <ScriptProjects>
            <ScriptComponentProject Name="GetData">
                   <AssemblyReferences>
                    <AssemblyReference AssemblyPath="Microsoft.SqlServer.DTSPipelineWrap" />
                    <AssemblyReference AssemblyPath="Microsoft.SqlServer.DTSRuntimeWrap" />
                    <AssemblyReference AssemblyPath="Microsoft.SqlServer.PipelineHost" />
                    <AssemblyReference AssemblyPath="Microsoft.SqlServer.TxScript" />
                    <AssemblyReference AssemblyPath="System.dll" />
                    <AssemblyReference AssemblyPath="System.AddIn.dll" />
                    <AssemblyReference AssemblyPath="System.Data.dll" />
                    <AssemblyReference AssemblyPath="System.Xml.dll" />
                </AssemblyReferences>

                <ReadOnlyVariables>
                     <Variable VariableName="CalendarName" DataType="String" Namespace="User"  ></Variable>
                    <Variable VariableName="RootFolder" DataType="String" Namespace="User"  ></Variable>
                    <Variable VariableName="StartMonth" DataType="Int32" Namespace="User"  ></Variable>
                    <Variable VariableName="StartYear" DataType="Int32" Namespace="User"  ></Variable>
                    <Variable VariableName="EndMonth" DataType="Int32" Namespace="User"  ></Variable>
                    <Variable VariableName="EndYear" DataType="Int32" Namespace="User"  ></Variable>
                </ReadOnlyVariables>

                <OutputBuffers>
                    <OutputBuffer Name="CalendarOutput" IsSynchronous="false">
                        <Columns>
                           <Column Name="Detail" DataType="String" ></Column>
                           <Column Name="Status" DataType="String" Length="50" ></Column>
                           <Column Name="ActualStartDate" DataType="Date" ></Column>
                           <Column Name="Attendee" DataType="String"  Length="50" ></Column>
                           <Column Name="Location" DataType="String"  Length="500"></Column>
                           <Column Name="Subject" DataType="String"  Length="255" ></Column>
                           <Column Name="ActualEndDate" DataType="Date" ></Column>
                           <Column Name="Category" DataType="String"  Length="50"></Column>
                           <Column Name="DetailType" DataType="String"  Length="50"></Column>
                           <Column Name="CalendarName" DataType="String"  Length="50"></Column>
                        </Columns>                    
                    </OutputBuffer>                                 
                </OutputBuffers>

                <Files >
                    <File Path="Properties\AssemblyInfo.cs">
                        using System.Reflection;
                        using System.Runtime.CompilerServices;
                        [assembly: AssemblyTitle("GetData")]
                        [assembly: AssemblyDescription("Get calendar data from Exchange")]
                        [assembly: AssemblyConfiguration("")]
                        [assembly: AssemblyCompany("HCC DataServices")]
                        [assembly: AssemblyProduct("GetData")]
                        [assembly: AssemblyCopyright("Copyright @ 2017")]
                        [assembly: AssemblyTrademark("")]
                        [assembly: AssemblyCulture("")]
                        [assembly: AssemblyVersion("1.0.*")]
                    </File>

                    <ExternalFile Path="main.cs"  ExternalPath="ExchangeCalendarScript.cs"/>

                </Files>
            </ScriptComponentProject>
        </ScriptProjects>


    <Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Packages>
            <Package Name="<#=PackageName#>" ProtectionLevel="EncryptSensitiveWithUserKey">
    <Tasks>  
    <Dataflow Name="Get Calendar data">
                        <Transformations>
                            <ScriptComponentSource Name="Get calendar data">

                               <ScriptComponentProjectReference ScriptComponentProjectName="GetData"></ScriptComponentProjectReference>
                            </ScriptComponentSource>
                        </Transformations>
    </Dataflow>
    </Tasks>
    </Package>
        </Packages>
    </Biml>

我该如何解决?

4

0 回答 0