---
title: "ウォークスルー:パイプラインステップの実装"
slug: "walkthrough--implementing-a-pipeline-step"
abstract: "カスタムプロバイダーを作成するためのカスタムパイプラインステップをSitecore Data Exchange Frameworkで実装するためのガイド。"
---
# ウォークスルー:パイプラインステップの実装

このウォークスルーは「 [テキストファイルのカスタムプロバイダーを実装](https://doc.sitecore.com/xp/ja/developers/def/80/data-exchange-framework/implementing-a-custom-provider-for-text-files-walkthrough-series.html "Implementing a custom provider for text files walkthrough series") するウォークスルー」シリーズの第3部であり、パイプラインステップの実装方法を説明しています。パイプラインステップはデータ同期プロセス中に実行されるロジックを表します。この例では、パイプラインステップはテキストファイルの行を読み取る能力を表しています。

このウォークスルーでは、以下の方法を説明します:

-   パイプラインステップのテンプレートを作成する
-   パイプラインステップテンプレートにフィールドを追加する
-   パイプラインステップコンバータの実装
-   パイプラインステッププロセッサの実装
-   パイプラインステップテンプレートでデフォルト値を設定します

## パイプラインステップのテンプレートを作成する

テキストファイルからデータを読み取るにはパイプラインステップを使用します。どのテキストファイル(エンドポイント)から読み取るかを指定するにはテンプレートが必要です。

テンプレートを作成するには:

1.  コンテンツエディターで*Templates*に移動します。
    
2.  メニューリボンの **ホーム** タブの **「Insert** 」グループで「 **新しいテンプレート** 」をクリックして新しいテンプレートを追加してください。
    
    ![New Template option on the menu ribbon](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/def/80/data-exchange-framework/images/add-template.png)
    
3.  「 **名前選択** 」ダイアログで、以下の値を入力してください:
    
    フィールド
    
    価値
    
    **名称**
    
    *Read Text File Pipeline ステップ*
    
    **ベーステンプレート**
    
    *テンプレート/データ交換/フレームワーク/パイプラインステップ/ベースパイプラインステップ/ベースパイプラインステップ*
    
4.  **次へ**クリック。
    
5.  **ロケーション**ダイアログで*Templates/Data Exchange/Providers/File System/Pipeline Steps*を選択します。**「次へ**」をクリックします。
    
6.  確認ダイアログで **「閉じる**」をクリックしてください。
    
7.  新しいテンプレートのアイコンを設定するには、コンテンツツリー内でテンプレートを選択してください。例えば、*テンプレート/データ交換/プロバイダー/ファイルシステム/パイプラインステップ/テキストファイルパイプラインの読み取り*ステップなどです。
    
8.  右側のペインの **「コンテンツ** 」タブで、タイトルの横にあるアイコンをクリックしてください。
    
    ![Setting the icon for a pipeline step](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/def/80/data-exchange-framework/images/icon2.png)
    
9.  アイコン **変更** ダイアログの **アイコン** フィールドで、例えば Office/32x32/element.png
    

## パイプラインステップテンプレートにフィールドを追加する

パイプラインステップテンプレートでは、Sitecoreにどのエンドポイントを使うかを設定する必要があります。テンプレートフィールドを追加することでこれを行います。

テンプレートフィールドを追加するには:

1.  コンテンツツリーでパイプラインステップテンプレートを選択してください。テンプレートセクションを追加し、*Endpoints*と名付けます。
    
2.  **エンドポイント**でテンプレートフィールドを追加し、名前を*EndpointFrom*とします。以下のフィールドを埋めてください:
    
    フィールド
    
    価値
    
    **名称**
    
    *EndpointFrom*
    
    **種類**
    
    *Droptree*
    
    **出典**
    
    query:./ancestor-or-self::\*@@templateid='{327A381B-59F8-4E88-B331-BEBC7BD87E4E}'//descendant-or-self::\*@@templateid='TEMPLATE-ID'
    
    **共有**
    
    選ばれました
    
    > !重要ソースフィールドでは、TEMPLATE-IDを*File System Endpoints Root*と名付けられたテンプレートのItem IDに置き換える必要があります。このテンプレートは、プロバイダーのエンドポイントアイテムを格納するためのフォルダを表しています。
    > 
    > このテンプレートは、Data Exchange Framework SDKを使ってプロバイダーを作成する際に自動的に作成されました。テンプレートは */sitecore/templates/Data Exchange/Providers/File System/Folders/File System Endpoints Rootにあります*。IDはコンテンツタブで見つけることができます。
    
    **ソース**フィールドのクエリは、ドロップツリーフィールドのタイプを、このプロバイダーのエンドポイントを整理するために使われるフォルダ内の現在のテナント内のエンドポイントに限定します。
    
    ID {327A381B-59F8-4E88-B331-BEBC7BD87E4E} はフレームワークに付属するテナントテンプレートのものです。この値は変更してはいけません。
    
3.  **共有**チェックボックスを選択してください。商品を保存してください。
    
    > !注アイテムを保存すると、そのアイテムに壊れたリンクが含まれていることがメッセージで表示されます。これはステップ2で設定した式に関連する期待される動作です。「OK」をクリックして続けてください。
    

## パイプラインステップコンバータの実装

パイプラインステップの項目の設定をプラグインに変換するにはコンバーターを使う必要があります。プラグインはパイプラインステップが読み取るエンドポイントを特定します。

パイプラインステップコンバータの実装方法:

1.  Visual Studioで、次のクラスを追加します:
    
    using Sitecore.DataExchange.Attributes;
    using Sitecore.DataExchange.Converters.PipelineSteps;
    using Sitecore.DataExchange.Models;
    using Sitecore.DataExchange.Plugins;
    using Sitecore.DataExchange.Repositories;
    using Sitecore.Services.Core.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Examples.DataExchange.Providers.FileSystem
    {
     SupportedIds(ReadTextFileStepTemplateId)
     public class ReadTextFileStepConverter : BasePipelineStepConverter
     {
     public const string ReadTextFileStepTemplateId = "PIPELINE STEP TEMPLATE ID";
     public const string TemplateFieldEndpointFrom = "EndpointFrom";
     public ReadTextFileStepConverter(IItemModelRepository repository) : base(repository)
     {
     }
     protected override void AddPlugins(ItemModel source, PipelineStep pipelineStep)
     {
     //
     //create the plugin
     var settings = new EndpointSettings
     {
     //
     //populate the plugin using values from the item
     EndpointFrom = this.ConvertReferenceToModel<Endpoint>(source, TemplateFieldEndpointFrom)
     };
     //
     //add the plugin to the pipeline step
     pipelineStep.AddPlugin(settings);
     }
     }
    }
    
    > !重要PIPELINE STEP TEMPLATE IDの値を、パイプラインステップテンプレートの *「Read Text File Pipeline Step*」のIDに合わせて変更する必要があります。
    
2.  プロジェクトをコンパイルします。
    
3.  Examples.DataExchange.Providers.FileSystem.dllをSitecoreサーバーにデプロイしてください。
    

BasePipelineStepConverter(base-pipeline-step-converter.md "Base pipeline step converter")から継承することで、Sitecoreアイテムのフィールドから値を読み取るための多くの手法にアクセスできます。

文字列、数字、日付などの単純な値を読み取れるだけでなく、参照された項目を自動的に適切なData Exchange Frameworkコンポーネントに変換するメソッドも利用可能です。

## パイプラインステッププロセッサの実装

プロセッサを使ってコンポーネントのビジネスロジックを実装します。この例では、パイプラインステップのビジネスロジックを実装するためにプロセッサが必要です。このステップは、パイプラインステップに割り当てられたエンドポイントに対応するテキストファイルからデータを読み取る役割を担っています。

パイプラインステップは真空状態で存在するわけではありません。それはデータ同期プロセスの一部として設計されています。つまり、パイプラインステップを実行する際にプロセッサが特定の条件を要求する場合があります。この例では、プロセッサはエンドポイントのプラグインコレクションに*TextFileSettings*プラグインを含めることを求めています。

プロセッサは、その後のパイプラインステップで必要となる条件を作成することもあります。この例では、プロセッサがパイプラインコンテキストのプラグインコレクションにプラグイン*IterableDataSettings*を追加します。

パイプラインステッププロセッサを実装するには:

1.  Visual Studioで、次のクラスを追加します:
    
    using Sitecore.DataExchange.Attributes;
    using Sitecore.DataExchange.Contexts;
    using Sitecore.DataExchange.Models;
    using Sitecore.DataExchange.Plugins;
    using Sitecore.DataExchange.Processors.PipelineSteps;
    using Sitecore.Services.Core.Diagnostics;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Examples.DataExchange.Providers.FileSystem
    {
     RequiredEndpointPlugins(typeof(TextFileSettings))
     public class ReadTextFileStepProcessor : BaseReadDataStepProcessor
     {
     public ReadTextFileStepProcessor()
     {
     }
     protected override void ReadData(
     Endpoint endpoint,
     PipelineStep pipelineStep,
     PipelineContext pipelineContext,
     ILogger logger)
     {
     if (endpoint == null)
     {
     throw new ArgumentNullException(nameof(endpoint));
     }
     if (pipelineStep == null)
     {
     throw new ArgumentNullException(nameof(pipelineStep));
     }
     if (pipelineContext == null)
     {
     throw new ArgumentNullException(nameof(pipelineContext));
     }
     if (logger == null)
     {
     throw new ArgumentNullException(nameof(logger));
     }
     //
     //get the file path from the plugin on the endpoint
     var settings = endpoint.GetTextFileSettings();
     if (settings == null)
     {
     return;
     }
     if (string.IsNullOrWhiteSpace(settings.Path))
     {
     logger.Error(
     "No path is specified on the endpoint. " +
     "(pipeline step: {0}, endpoint: {1})",
     pipelineStep.Name, endpoint.Name);
     return;
     }
     if (!File.Exists(settings.Path))
     {
     logger.Error(
     "The path specified on the endpoint does not exist. " +
     "(pipeline step: {0}, endpoint: {1}, path: {2})",
     pipelineStep.Name, endpoint.Name, settings.Path);
     return;
     }
     //
     //add the data that was read from the file to a plugin
     var data = this.GetIterableData(settings);
     var dataSettings = new IterableDataSettings(data);
     //
     //add the plugin to the pipeline context
     pipelineContext.AddPlugin(dataSettings);
     }
     protected virtual IEnumerable<string> GetIterableData(TextFileSettings settings)
     {
     //
     //read the file, one line at a time
     var separator = new string { settings.ColumnSeparator };
     using (var reader = new StreamReader(File.OpenRead(settings.Path)))
     {
     var firstLine = true;
     while (!reader.EndOfStream)
     {
     var line = reader.ReadLine();
     if (firstLine && settings.ColumnHeadersInFirstLine)
     {
     firstLine = false;
     continue;
     }
     if (string.IsNullOrWhiteSpace(line))
     {
     continue;
     }
     //
     //split the line into an array, using the separator
     var values = line.Split(separator, StringSplitOptions.None);
     yield return values;
     }
     }
     }
     }
    }
    
    > !注[BaseReadDataStepProcessor](https://doc.sitecore.com/xp/ja/developers/def/80/data-exchange-framework/pipeline-step-processors.html "Pipeline step processors")から継承することで、エンドポイントからデータを読み取るのを容易にするメソッドにアクセスできます。
    > 
    > ベースクラスは読み取ったデータに何が起こるかを指定していません。代わりに、データは*IterableDataSettings*プラグインに追加されます。その後のパイプラインステッププロセッサを実装してデータを反復し、処理方法を指定することができます。
    
2.  プロジェクトをコンパイルします。
    
3.  Examples.DataExchange.Providers.FileSystem.dllをSitecoreサーバーにデプロイしてください。
    

## パイプラインステップテンプレートでデフォルト値を設定します

パイプラインステップテンプレートを使う場合は、テンプレートにデフォルト値を追加する必要があります。

テンプレートにデフォルト値を追加するには:

1.  コンテンツエディターで*Templates/Data Exchange/Providers/File System/Endpoints/Endpoint*に移動します。
    
2.  右側のペインで「 **ビルダー** 」タブをクリックします。
    
3.  メニューリボンの**Builder OPTIONS**タブの **テンプレート** グループで「 **Standard value**」をクリックします。
    
    ![Standard values in Templates group on menu ribbon](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/def/80/data-exchange-framework/images/standard-values-button.png)
    
4.  新しいアイテムを選択してください。
    
    ![Standard values in content tree](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/def/80/data-exchange-framework/images/standard-values-item2.png)
    
5.  右側のペインの **「コンテンツ** 」タブで、以下のデフォルトのフィールド値を設定します:
    
    フィールド
    
    価値
    
    **コンバータータイプ**
    
    *例:DataExchange.Providers.FileSystem.ReadTextFileStepConverter, Examples.DataExchange.Providers.FileSystem*
    
    **プロセッサタイプ**
    
    *例:DataExchange.Providers.FileSystem.ReadTextFileStepProcessor、例:DataExchange.Providers.FileSystem*
    
    > !注コンバータータイプフィールドはデータ交換フレームワークのセクションにあります。フィールドを見る前にセクションを拡張する必要があるかもしれません。
    
6.  アイテムを保存してください。