---
title: "攻略:Stylelabs.Mの使用。SDK。ウェブクライアントSDKを流暢に使う"
slug: "walkthrough--using-stylelabs-m-sdk-fluent-through-the-web-client-sdk"
abstract: "プロジェクトの作成方法、ベースクラスの作成、エンティティの作成、エンティティのクエリ方法。"
---
# 攻略:Stylelabs.Mの使用。SDK。ウェブクライアントSDKを流暢に使う

このセクションでは以下の内容を案内します。

1.  [プロジェクトを作成してください](https://doc.sitecore.com/ch/ja/developers/cloud-dev/walkthrough--using-stylelabs-m-sdk-fluent-through-the-web-client-sdk.html#UUID-28fcc83e-ebd6-4c80-fc3c-1e8e7f603f3b_N1708429404669 "Step 1")
2.  [基底クラスを作成する](https://doc.sitecore.com/ch/ja/developers/cloud-dev/walkthrough--using-stylelabs-m-sdk-fluent-through-the-web-client-sdk.html#UUID-28fcc83e-ebd6-4c80-fc3c-1e8e7f603f3b_N1708429438683 "Step 2")
3.  [エンティティを作成する](https://doc.sitecore.com/ch/ja/developers/cloud-dev/walkthrough--using-stylelabs-m-sdk-fluent-through-the-web-client-sdk.html#UUID-28fcc83e-ebd6-4c80-fc3c-1e8e7f603f3b_N1708429452957 "Step 3")
4.  [クエリエンティティ](https://doc.sitecore.com/ch/ja/developers/cloud-dev/walkthrough--using-stylelabs-m-sdk-fluent-through-the-web-client-sdk.html#UUID-28fcc83e-ebd6-4c80-fc3c-1e8e7f603f3b_N1708429464972 "Step 4")

## プロジェクトを作成してください

> !注以下の例は.NET Core CLIを使用しています。お好みのIDEで同等の機能を利用できます。

新しいプロジェクトを作成するには、以下のコマンドを実行します。

mkdir Walkthrough
 cd Walkthrough
 dotnet new integration-tests --framework net6.0

プロジェクトが作成されると、Content Hub SDK Fluentライブラリへの参照が必要です:

dotnet add package Stylelabs.M.Sdk.Fluent.Testing --source https://nuget.sitecore.com/resources/v3/index.json

## 基底クラスを作成する

> !注これらの例は[xUnit](https://xunit.net/)をテストフレームワークとして使用しています

共通機能すべてを扱うベースクラスを作成し、例えばSDKクライアントの作成などを考えます。

using System;
using Stylelabs.M.Sdk.Fluent;
using Stylelabs.M.Sdk.WebClient.Authentication;

namespace Integration.Tests.Walkthrough
{
 public abstract class TestBase
 {
 protected readonly IWebMClient \_mClient;
 protected readonly FluentClient \_fluentClient;

 protected TestBase()
 {
 \_mClient = CreateMClient();
 \_fluentClient = new FluentClient(\_mClient);
 }

 private static IWebMClient CreateMClient()
 {
 var endpoint = new Uri(Configuration.Authentication.Url);

 var oath = new OAuthPasswordGrant
 {
 ClientId = Configuration.Authentication.ClientId,
 ClientSecret = Configuration.Authentication.ClientSecret,
 Password = Configuration.Authentication.Password,
 UserName = Configuration.Authentication.Username
 };

 return Stylelabs.M.Sdk.WebClient.MClientFactory.CreateMClient(endpoint, oath);
 }
 }
}

## エンティティを作成する

新しいテストクラスを作成しましょう。このクラスのテストは以下の通りです:

-   新しい Entity Definition を作り、そのメンバーを認証しましょう
-   新しい Entity を作り、そのメンバーを認証しましょう

using System.Collections.Generic;
using System.Globalization;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.Models.Base.PropertyDefinitions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;

namespace Sdk.Fluent.Testing.Walkthrough
{
 public class NewEntities : FluentTestBase
 {
 Fact
 public void Create\_New\_EntityDefinition()
 {
 var definition = \_fluentClient.EntityDefinitions
 .New("My.Definition")
 .NewMemberGroup("MyGroup", overviewGroup = overviewGroup
 .AddPropertyDefinition<StringPropertyDefinition> (
 "myStringProperty",
 stringPropertyDefinition =
 {
 stringPropertyDefinition.IncludedInContent = true;
 stringPropertyDefinition.IsMultiValue = false;
 stringPropertyDefinition.Labels = new Dictionary<CultureInfo, string>
 { { CultureInfo.InvariantCulture, "myStringProperty" } };
 })
 .AddPropertyDefinition<BooleanPropertyDefinition>("myBooleanProperty")
 .AddPropertyDefinition<IntegerPropertyDefinition>("myIntProperty"))
 .Save();

 definition
 .AssertMemberGroup("MyGroup")
 .MemberGroup("MyGroup", group = group
 .AssertPropertyExists("myStringProperty")
 .AssertPropertyExists("myBooleanProperty")
 .AssertPropertyExists("myIntProperty"));
 }

 Fact
 public void Create\_New\_Entity()
 {
 var entity = \_fluentClient.EntityFactory
 .Create("My.Definition")
 .SetProperty("myStringProperty", "this is an string value")
 .SetProperty("myBooleanProperty", true)
 .SetProperty("myIntProperty", 100)
 .Save();

 entity
 .AssertPropertyEqual("myBooleanProperty", true)
 .AssertPropertyNotEmpty("myStringProperty")
 .AssertPropertyGreaterOrEqual("myIntProperty", 100);
 }
 }
}

## クエリエンティティ

新しいテストクラスを作成しましょう。このクラスのテストは以下の通りです:

-   エンティティ定義による Entities クエリ
-   idによるクエリEntity
-   多くのクエリ Entities

using Stylelabs.M.Sdk.Fluent.Models;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;

namespace Sdk.Fluent.Testing.Walkthrough
{
 public class QueryEntities : FluentTestBase
 {
 Fact
 public void Query\_Entity\_ByEntityDefinition()
 {
 var entities = \_fluentClient.Entities
 .GetByDefinition("My.Definition");

 foreach (FluentEntity entity in entities)
 {
 entity
 .AssertPropertyEqual("myBooleanProperty", true)
 .AssertPropertyNotEmpty("myStringProperty")
 .AssertPropertyEqual("myIntProperty", 100);
 }
 }

 Fact
 public void Query\_Many\_Entities()
 {
 var entities = \_fluentClient.Entities
 .GetMany(new long { 12345, 67890 }); // this should be a list of valid ids

 foreach (FluentEntity entity in entities)
 {
 entity
 .AssertPropertyEqual("myBooleanProperty", true)
 .AssertPropertyNotEmpty("myStringProperty")
 .AssertPropertyEqual("myIntProperty", 100);
 }
 }

 Fact
 public void Query\_Entity\_ById()
 {
 \_fluentClient.Entities
 .Get(12345) // this should be a valid id
 .AssertPropertyEqual("myBooleanProperty", true)
 .AssertPropertyNotEmpty("myStringProperty")
 .AssertPropertyNotEqual("myIntProperty", 10);
 }
 }
}