---
title: "インポートパイプラインを拡張する"
slug: "extend-the-import-pipeline"
abstract: "カスタムプロセッサを使ったインポートパイプラインの動作を変更する方法の例"
---
# インポートパイプラインを拡張する

コードファーストワークフローの [インポートパイプライン](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/pipelines-used-in-the-jss-code-first-import-process.html "Pipelines used in the JSS code-first import process") を拡張し、アプリケーションのデータのさまざまな側面をカスタマイズできます。

この手順では、インポートプロセスを拡張して作成したテンプレートのパスをカスタマイズする方法を示しています。

インポートパイプラインを拡張するには:

1.  [JSSマニフェストにコンポーネントを追加](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/adding-manifest-definitions-in-code-first-jss-apps.html "Adding Manifest definitions in code-first JSS apps")してマニフェストデータを拡張しましょう。
    
    export default manifest => {
     manifest.addComponent({
     name: "Welcome",
     // we added this property to our welcome component
     path: "/sitecore/templates/Welcome",
     fields: 
     { name: "title", type: manifest.fieldTypes.singleLineText },
     
     });
    };
    
2.  カスタムパスを機能させるには、インポートパイプラインを拡張し、インポートプロセスによるテンプレート作成方法を変更する必要があります。具体的には、importパイプライン内のProcessTemplatesプロセッサをターゲットにし、そこから派生し、その挙動を変更する必要があります。例えば:
    
    using System;
    using Sitecore.Data.Items;
    using Sitecore.Diagnostics;
    using Sitecore.JavaScriptServices.AppServices.Data;
    using Sitecore.JavaScriptServices.AppServices.Extensions;
    using Sitecore.JavaScriptServices.AppServices.Models;
    using Sitecore.JavaScriptServices.Configuration;
    
    namespace Sitecore.JavaScriptServices.AppServices.Pipelines.Import
    {
     public class ProcessTemplatesWithPath : ProcessTemplates
     {
     protected override TemplateItem CreateTemplate(TemplateDef templateDef, Item parent, Item multilistRoot, AppConfiguration app, IdManager idManager)
     {
     // the AdditionalData property is a dictionary of any JSON properties that were \_not\_ mapped to a TemplateDef property
     // so our path property - and any other custom properties that are added - will show up here. It will be null if no unmapped
     // properties exist so make sure to check everything for nulls with ?
     //
     // All import pipeline model objects have an AdditionalData property like this.
     var path = templateDef.AdditionalData?"path"?.ToString();
    
     // path not set, use default behavior
     if (path == null) return base.CreateTemplate(templateDef, parent, multilistRoot, app, idManager);
    
     // resolve the custom path's parent (to keep things simple, we're not creating any parent hierarchy)
     var parentItemPath = path.Substring(0, path.LastIndexOf("/"));
     var customParentItem = parent.Database.GetItem(parentItemPath, parent.Language);
    
     if (customParentItem == null) throw new InvalidOperationException($"Parent item for custom import path item {templateDef.Name} ({path}) did not exist.");
    
     var templateName = ItemUtil.ProposeValidItemName(templateDef.Name);
    
     // JSS imports use \_consistent IDs\_ so that imports across instances use the same Sitecore item GUIDs
     // This code generates the consistent ID and ensures that it is unique.
     var templateId = idManager.GetTemplateId(app.Name, templateDef);
     idManager.AssertIdIsUnused(templateId, $"template {templateDef.Name}");
    
     // ensure we have permissions to create the item
     if (!customParentItem.CanImportChild(templateName, templateId, out var templateItem))
     {
     return null;
     }
    
     if (templateItem == null)
     {
     Log.Info($"JSS - Creating template - {templateName} (Custom path: {path})", this);
     templateItem = parent.Database.Templates.CreateTemplate(templateName, customParentItem);
    
     // make the db see our new template
     parent.Database.Engines.TemplateEngine.Reset();
     }
     else
     {
     Log.Debug($"JSS - Found existing template - {templateName}", this);
     }
    
     // sets the requisite fields on the template item
     UpdateTemplateItem(templateItem, templateDef, parent, multilistRoot, app, idManager, templateItem.DisplayName);
    
     return templateItem;
     }
     }
    }
    
3.  今度はSitecoreにデフォルトのProcessTemplatesプロセッサではなくカスタムプロセッサProcessTemplatesWithPathを使うよう指示しなければなりません。例えばApp\_Config/Include/PickAnyFileName.config設定パッチを作成し、以下の構成を追加してください。
    
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
     <sitecore role:require="ContentManagement or Standalone">
     <pipelines>
     <group groupName="javaScriptServices">
     <pipelines>
     <import>
     <processor
     type="Sitecore.JavaScriptServices.AppServices.Pipelines.Import.ProcessTemplatesWithPath, Sitecore.JavaScriptServices.AppServices"
     patch:instead="\*contains(@type, 'ProcessTemplates')" />
     </import>
     </pipelines>
     </group>
     </pipelines>
     </sitecore>
    </configuration>
    
4.  JSSアプリをデプロイする:
    
    -   もしこれが初めての展開であれば、ターミナルで以下のコマンドを実行してください:
        
        jss deploy app -c
        
    -   それ以外の場合は、以下のコマンドを実行します。
        
        jss deploy items
        
    
    > !注スクリプトを実行すると、JSS自動的にファイルsitecore/manifest/sitecore-import.jsonに[マニフェストが生成されます](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/generating-a-jss-app-manifest-file.html "Generating a JSS app manifest file")。
    
    スクリプトは以下のようなメッセージを出力します。
    
    JSS - Creating template - Welcome (Custom path: /sitecore/templates/Welcome)
    
5.  Sitecoreのコンテンツツリーで、テンプレートがリクエストしたパスで作成されたか確認してください。
    
    > !注以前にJSSアプリをデプロイしていて、既存のテンプレート項目がデフォルトの場所にあった場合は、それを削除し、新しいテンプレートに使われているものを変更してください。