---
title: "getLayoutServiceContextパイプラインを拡張する"
slug: "extend-the-getlayoutservicecontext-pipeline"
abstract: "自分のデータもSitecoreコンテキストオブジェクトに追加してください"
---
# getLayoutServiceContextパイプラインを拡張する

レイアウトサービスが提供するgetLayoutServiceContextパイプラインを拡張することで、標準プロパティを有効化・無効化したり、自分のデータをcontextオブジェクトに追加したりできます。

getLayoutServiceContextパイプラインを拡張するには:

1.  Headless Servicesが提供するJssGetLayoutServiceContextProcessorベースを拡張してプロセッサを作成する:
    
    > !注Headless Services以外でレイアウトサービスの利用者に拡張機能を提供したい場合は、Sitecore.LayoutService.ItemRendering.Pipelines.GetLayoutServiceContext.IGetLayoutServiceContextProcessorインターフェースを実装できます。
    
    public class ExampleContextExtension : Sitecore.JavaScriptServices.ViewEngine.LayoutService.Pipelines.GetLayoutServiceContext.JssGetLayoutServiceContextProcessor
    {
     public ExampleContextExtension(IConfigurationResolver configurationResolver) : base(configurationResolver)
     {
     }
    
     protected override void DoProcess(GetLayoutServiceContextArgs args, AppConfiguration application)
     {
     args.ContextData.Add("securityInfo", new
     {
     isAnonymous = !Sitecore.Context.User.IsAuthenticated
     });
     }
    }
    
2.  プロセッサにパッチを当て設定するには、プロセッサ用の設定パッチを作成し、プロセッサが実行すべきアプリやルートを設定してください。以下の例で示されています。
    
    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
     <sitecore>
     <pipelines>
     <group groupName="layoutService">
     <pipelines>
     <getLayoutServiceContext>
     <processor type="SitecoreJss.Examples.ExampleContextExtension, SitecoreJss.Examples" resolve="true">
     <AllowedConfigurations hint="list">
     <!-- Unless you change the Layout Service config used by your JSS app, this should always be present. -->
     <jss>jss</jss>
     </AllowedConfigurations>
     <Applications hint="list">
     <!-- Restrict the JSS apps for which this processor will execute. -->
     <reactApp>JssReactWeb</reactApp>
     </Applications>
     <Routes hint="list">
     <!-- Restrict the route items for which this processor will execute. IDs, item paths, and wildcard item paths are allowed. -->
     <services>/sitecore/content/JssReactWeb/Home/Services\*</services>
     <portfolio>{BAD2C001-1746-4312-8422-B28806A1191E}</portfolio>
     </Routes>
     </processor>
     </getLayoutServiceContext>
     </pipelines>
     </group>
     </pipelines>
     </sitecore>
    </configuration>
    
3.  修正されたコンテキストの出力を確認するために、レイアウトサービスのエンドポイントを呼び出しhttp://{YOUR\_SITECORE\_HOST}/sitecore/api/layout/render/jss?item=/Services&sc\_apikey={YOUR\_API\_KEY}:
    
    "context": {
     "securityInfo": {
     "isAnonymous": true
     },
     "pageEditing": false,
     "site": {
     "name":"JssReactWeb"
     },
     "navigation": 
     {
     "name":"Home",
     "path":"/",
     "children": 
     {
     "name":"About",
     "path":"/about"
     },
     {
     "name":"Portfolio",
     "path":"/portfolio"
     },
     {
     "name":"Services",
     "path":"/services"
     }
     
     }
     
    }