---
title: "Sitecoreレイアウトサービス"
slug: "sitecore-layout-service"
abstract: "Sitecoreレイアウトサービスのリクエストおよびレスポンスモデル、レイアウトサービスクライアントの設定、リクエスト操作の概要。"
---
# Sitecoreレイアウトサービス

[Sitecoreレイアウトサービス](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/layout-service.html "Layout Service")は、JSONフォーマットされたSitecoreコンテンツを提供する[Sitecoreヘッドレスサービスの](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/sitecore-headless-services.html "Sitecore Headless Services")エンドポイントです。レイアウトサービスと通信するにはSitecoreレイアウトサービスクライアントを使用し、コンテンツのレンダリングには[Sitecoreレンダリングエンジン](https://doc.sitecore.com/xp/ja/developers/hd/19/sitecore-headless-development/asp-net-core-rendering-engine.html "ASP.NET Core Rendering Engine")を使用します。

レイアウトサービスクライアントライブラリはNuGetパッケージとして利用可能で、その名前はSitecore.LayoutService.Clientで始まります。

## SitecoreLayoutRequestモデル

SitecoreLayoutRequestモデルは任意のプロパティの読み書きをサポートする中間データ転送オブジェクトにインスタンス化されます。レイアウトサービスクライアントはクエリ文字列キーを使ってこれらのプロパティをレイアウトサービスに送信します。

SitecoreLayoutRequestモデルは、get/setメソッドを組み合わせたデフォルトプロパティのセットを含みます。例えば、request.ApiKey()はAPIキー値を取得し、request.ApiKey("value")はAPIキー値を設定します。

デフォルトのプロパティは以下の通りです:

財産

方法

内容

サンプル

クエリ文字列キー

APIキー(必須)

ApiKey()

[ガイド](https://en.wikipedia.org/wiki/Universally_unique_identifier)

123e4567-e89b-12d3-a456-426614174000

sc\_apikey

アイテム(必須)

Path()

コンテンツツリーパス

/articles

item

遺跡名

SiteName()

人間が読みやすい名前

MySite

sc\_site

言語

Language()

[ISO言語コード](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)

en、en-GB。

sc\_lang

## SitecoreLayoutResponseモデル

レイアウトサービスは、フィールドレベルまでのナビゲーションを可能にするように設計された強型モデルSitecoreLayoutResponseからインスタンス化されたオブジェクトを返します。SitecoreLayoutResponseオブジェクトは以下のいくつかの部分で構成されています。

-   Request\- レイアウトサービスに送られる SitecoreLayoutRequest 。
-   Metadata\- HTTP応答ヘッダーなどのレスポンスメタデータ。
-   Content\- 非直列化されたJSON応答。
-   Errors\- SitecoreLayoutRequest オブジェクトの扱いおよびSitecoreLayoutResponseオブジェクトの構築中に発生したエラーのリスト。
-   HasErrors\- これはErrorsリストが空でない場合に成り立ちます。

### 種類と分野

Content以下のタイプを使用します:

-   Route\- レイアウトサービスが返すJSON応答のRouteオブジェクトにマッピング。そのフィールドはDictionary<string, IFieldReader>としてタイプ付けされています。
-   Component\- プレースホルダーのリスト値にオブジェクトをマッピングする。そのフィールドはDictionary<string, IFieldReader>として型付けされています。
-   IFieldReader\-RouteまたはComponentのフィールドエントリへのマッピング。この実装はNewtonsoftのようなシリアライズライブラリによって提供されています。
-   IField\- Layout Service Clientによって様々なIField実装が提供されています。インターフェースは、IFieldReaderを通って読み取れるタイプを特定するマーカーとして使われます。

レイアウトサービスクライアントは以下のフィールドを提供します:

-   CheckboxField\- チェックボックス欄。
-   ContentListField\- アイテムリンクフィールドのリスト。
-   DateField\- 日付と時間のフィールド。
-   FileField\- ファイルフィールド。
-   HyperLinkField\- リンクフィールド。
-   ImageField\- イメージフィールド。
-   ItemLinkField\- 単一の項目フィールド。
-   NumberFieldc - 数の領域。
-   RichTextField\- リッチテキストフィールド。
-   TextField\- 単行または複数行のテキストフィールド。

フィールドは複数のインターフェースを実装し、例えば以下の異なるフィールド特性を公開できます。

-   IValueField<T>\- 型TのValue性質を露出させる。
-   IEditableField\- 編集可能なフィールドの前後のマークアップを公開します。

### 構文論

IFieldReader型はRead()メソッドを公開し、IFieldを実装する型が与えられると、IFieldReaderからのデータで埋められた型のインスタンスを返します。

ComponentRoute両者は同じ基本実装を受け継ぎ、Fieldsを露出させます。さらに、以下を露出します:

-   ReadField() - 特定のフィールドを読むためのIField型とstringを受け取る。
-    ReadFields()\- 任意の型を受け取り、プロパティ名とフィールド名が一致するすべてのフィールドで埋められます。

## ASP.NET用のRESTレイアウトサービスクライアント設定

以下の例は、アプリケーションStartupクラスのConfigureServicesメソッドでASP.NETレンダリングSDKを用いたレイアウトサービスクライアントの使用例を示しています。

services
 .AddRouting()
 .AddMvc()

 // Configure the required Newtonsoft.Json serialization behavior for use
 // with the Layout Service Client.
 // The Layout Service Client requires Json.NET due to limitations in System.Text.Json.
 .AddNewtonsoftJson(o => o.SerializerSettings.SetDefaults());

 // Register the Sitecore Layout Service Client,
 // which is used by the ASP.NET Rendering Engine.
 .AddSitecoreLayoutService()

 // Configure the default parameters for Layout Service Client requests.
 .WithDefaultRequestOptions(request =>
 {
 request

 // Optional language configuration, if the application is not using localized routes.
 // .Language("en")

 // The name of the Sitecore site which this application is rendering.
 .SiteName("website")

 .ApiKey("{00000000-0000-0000-0000-000000000000}");
 })

 // Provide the URL to the Layout Service on your Sitecore instance.
 // In production, this is typically a Content Delivery server.
 // In development, it might be a standalone combined Content Management
 // and Content Delivery instance.
 .AddHttpHandler("default", "https://<sitecore instance>/sitecore/api/layout/render/jss")
 .AsDefaultHandler();

レイアウトサービスにアクセスするには、[Sitecoreサービスのクライアントのキー API ID](https://doc.sitecore.com/xp/en/developers/102/sitecore-experience-manager/api-keys-for-the-odata-item-service.html)ApiKeyメソッドを提供する必要があります。