サーバー オブジェクト

サーバー ロジックは、サーバー名前空間の下に組み込みのオブジェクトを提供します。 これらのオブジェクトは、メッセージのログ記録、外部サービスの呼び出し、Dataverse の操作、または要求の詳細へのアクセスを可能にすることで、開発を簡略化します。

Httpクライアント

HTTP 要求を送信して外部サービスと統合するには、HTTP クライアントを使用します。

Note

現在、サーバー ロジックでは、要求本文で application/jsontext/html、および application/x-www-form-urlencoded コンテンツ タイプのみがサポートされています。

例示

HTTP GET

let url = "https://contoso.com/objects";
let header = { client_id: "00001111-aaaa-2222-bbbb-3333cccc4444" };

let response = await Server.Connector.HttpClient.GetAsync(url, header);

HTTP POST

let url = "https://contoso.com/objects";
let body = JSON.stringify({ name: "Sample Account" });
let header = { client_id: "00001111-aaaa-2222-bbbb-3333cccc4444" };
let contentType = "application/json";

// Make the POST request
let response = await Server.Connector.HttpClient.PostAsync(url, body, header, contentType);

HTTP PUT

let url = "https://contoso.com/objects/6";
let body = JSON.stringify({ name: "Updated Sample Account" });
let header = { client_id: "00001111-aaaa-2222-bbbb-3333cccc4444" };
let contentType = "application/json";

// Make the PUT request
let response = await Server.Connector.HttpClient.PutAsync(url, body, header, contentType);

HTTP PATCH

let url = "https://contoso.com/objects/6";
let body = JSON.stringify({ name: "{\"capacity\": \"2 TB\"}" });
let header = { client_id: "00001111-aaaa-2222-bbbb-3333cccc4444" };
let contentType = "application/json";

// Make the PATCH request
let response = await Server.Connector.HttpClient.PatchAsync(url, body, header, contentType);

HTTP の削除

let url = "https://contoso.com/objects/6";
let header = { contentType: "application/json" };

let response = await Server.Connector.HttpClient.DeleteAsync(url, header);

例: 応答

{
    "StatusCode": 200,
    "Body": "JsonString",
    "IsSuccessStatusCode": true,
    "ReasonPhrase": "OK",
    "ServerError": false,
    "ServerErrorMessage": null,
    "Headers": {
        "Transfer-Encoding": "chunked",
        "Connection": "keep-alive",
        "Server": "",
        "Content-Type": "application/json"
    }
}

SiteSetting

現在の Web サイトのサイト設定値を読み取ることができます。

Note

API キーや資格情報などのシークレットは、サーバー ロジックに直接格納しないでください。 代わりに、Azure Key Vaultに安全に格納し、環境変数を使用してソースを指定し、サイト設定を使用して参照します。

Example

Server.SiteSetting.Get("Search/Enabled");

EnvironmentVariable

環境変数の値を読み取ります。

Example

Server.EnvironmentVariable.get("SITEPATH");

ウェブサイト

Dataverse の現在の Web サイト レコードの詳細を提供します。

Example

Server.Website.adx_primarydomain;

User

サインインしているユーザーの詳細を提供します。 匿名の場合は null を返します。

Example

Server.User.fullname;

Dataverse

Server.Connector.Dataverse オブジェクトを使用して、Dataverse テーブルに対して CRUD 操作を実行し、カスタム API を呼び出します。

Note

  • コードで Dataverse テーブルを参照する場合は、 EntitySetName を使用します。 たとえば、 account テーブルにアクセスするには、EntitySetName accountsを使用します。

CreateRecord

新しいレコードを作成します。

Server.Connector.Dataverse.CreateRecord(string entitySetName, string payload)   

Example

Server.Connector.Dataverse.CreateRecord("accounts", "{\"name\": \"Contoso Ltd.\", \"telephone1\": \"555-555-0100\", \"websiteurl\": \"https://contoso.com\"}");

リトリーブレコード

ID で 1 つのレコードを取得します。

Server.Connector.Dataverse.RetrieveRecord(string entitySetName, string id)
Server.Connector.Dataverse.RetrieveRecord(string entitySetName, string id, string options)
Server.Connector.Dataverse.RetrieveRecord(string entitySetName, string id, string options, bool skipCache)

Example

Server.Connector.Dataverse.RetrieveRecord("accounts", "00000000-0000-0000-0000-000000000001", "$select=name,telephone1");

RetrieveMultipleRecords

レコードのコレクションを取得します。

Server.Connector.Dataverse.RetrieveMultipleRecords(string entitySetName)
Server.Connector.Dataverse.RetrieveMultipleRecords(string entitySetName, string options)
Server.Connector.Dataverse.RetrieveMultipleRecords(string entitySetName, string options, bool skipCache) 

Example

Server.Connector.Dataverse.RetrieveMultipleRecords("accounts", "$select=name,emailaddress1&$top=3");

UpdateRecord (更新レコード)

既存のレコードを ID で更新します。

Server.Connector.Dataverse.UpdateRecord(string entitySetName, string id, string payload)   

Example

Server.Connector.Dataverse.UpdateRecord("accounts", "00000000-0000-0000-0000-000000000001", "{ \"telephone1\": \"555-555-0100\" }");

DeleteRecord

レコードを ID で削除します。

Server.Connector.Dataverse.DeleteRecord(string entitySetName, string id) 

Example

Server.Connector.Dataverse.DeleteRecord("accounts", "00000000-0000-0000-0000-000000000001");

InvokeCustomApi

Server.Connector.Dataverse.InvokeCustomApi(string httpMethod, string url, string payload = null) 

バインドされた関数を呼び出します。

Server.Connector.Dataverse.InvokeCustomApi("get", "accounts(00000000-0000-0000-0000-000000000001)/Microsoft.Dynamics.CRM.new_CustomBoundFunction");

バインドされたアクションを呼び出します。

Server.Connector.Dataverse.InvokeCustomApi("post", "accounts(00000000-0000-0000-0000-000000000001)/Microsoft.Dynamics.CRM.new_CustomBoundAction", "{ \"parameter1\": \"value1\" }");

バインドされていないアクションを呼び出します。

Server.Connector.Dataverse.InvokeCustomApi("post", "new_Action", "{ \"parameter1\": \"value1\" }");

例: 応答

{
    "StatusCode": 204,
    "Body": "",
    "IsSuccessStatusCode": true,
    "ReasonPhrase": "No Content",
    "ServerError": false,
    "ServerErrorMessage": null,
    "Headers": {
        "x-ms-cds-service-request-id": "00001111-aaaa-2222-bbbb-3333cccc4444"
    }
}

Logger

ロガーを使用して、 DevTools 拡張機能で表示できる診断メッセージを記述します。

例:

Server.Logger.Log("Information message");
Server.Logger.Warn("Warning message");
Server.Logger.Error("Error message");

Context

Server.Context オブジェクトは、現在のサーバー ロジックの呼び出しに関する情報を提供します。 使用可能なプロパティは、サーバー ロジックが HTTP 要求を介して呼び出されたか、Liquid テンプレートから呼び出されたかによって異なります。

プロパティ

Name 対応対象 Description
ActivityId HTTP と Liquid サーバー ロジック呼び出しの一意識別子。 この値を使用して、ログを関連付け、操作のトラブルシューティングを行います。
Body HTTP 未加工の HTTP 要求本文。
FunctionName HTTP と Liquid 呼び出される JavaScript 関数の名前。 Liquid 呼び出しの場合、この値はserverlogic タグのoperation パラメーターに対応します。
Headers HTTP HTTP 要求ヘッダー。
HttpMethod HTTP HTTP 要求メソッド ( GETPOSTPUTPATCHDELETEなど)。
Input Liquid serverlogic Liquid タグのinput パラメーターを介して指定された入力文字列。 入力に構造化データが含まれている場合は、使用する前に JSON として解析します。
QueryParameters HTTP HTTP 要求からのクエリ文字列パラメーター。
ServerLogicName HTTP と Liquid 呼び出されるサーバー ロジック レコードの名前。
Url HTTP HTTP 要求の完全な URL。

HTTP 要求コンテキストにアクセスする

次の例では、HTTP 要求を介してサーバー ロジックが呼び出されたときに、 id クエリ パラメーターを読み取ります。

var id = Server.Context.QueryParameters["id"];

要求メタデータにアクセスすることもできます。

function getRequestInformation() {
    return JSON.stringify({
        activityId: Server.Context.ActivityId,
        functionName: Server.Context.FunctionName,
        httpMethod: Server.Context.HttpMethod,
        serverLogicName: Server.Context.ServerLogicName,
        url: Server.Context.Url
    });
}

Access Liquid の呼び出しコンテキスト

Liquid からサーバー ロジックが呼び出されたら、 Server.Context.Input を使用して、タグの input パラメーターを使用して指定された値を読み取ります。

たとえば、次の Liquid は JSON 入力を渡します。

{% assign inputData = '{"category":"active","maximumResults":5}' %}

{% serverlogic output: result, name: 'customer-summary', operation: 'getSummary', input: inputData %}

サーバー ロジック操作では、Liquid 呼び出しに関する入力情報とアクセス情報を解析できます。

function getSummary() {
    var input = JSON.parse(Server.Context.Input || "{}");

    return JSON.stringify({
        category: input.category,
        maximumResults: input.maximumResults,
        activityId: Server.Context.ActivityId,
        functionName: Server.Context.FunctionName,
        serverLogicName: Server.Context.ServerLogicName
    });
}

次のステップ

サーバー ロジックを使用して Dataverse テーブルを操作する方法

サーバー ロジックの概要
著者サーバーロジック