サーバー ロジックは、サーバー名前空間の下に組み込みのオブジェクトを提供します。 これらのオブジェクトは、メッセージのログ記録、外部サービスの呼び出し、Dataverse の操作、または要求の詳細へのアクセスを可能にすることで、開発を簡略化します。
Httpクライアント
HTTP 要求を送信して外部サービスと統合するには、HTTP クライアントを使用します。
Note
現在、サーバー ロジックでは、要求本文で application/json、 text/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テーブルにアクセスするには、EntitySetNameaccountsを使用します。
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"
}
}
CloudFlow
Server.Connector.CloudFlow オブジェクトを使用して、サーバー ロジックからPower Automateクラウド フローをトリガーします。 フローは既 にサイトに追加されている必要があります。 これは、Web ページから クラウド フロー API を使用してフローを呼び出すことと同等のサーバー側です。
/_api/cloudflow/v1.0/trigger/<guid> エンドポイントと同じ基になるパイプラインが使用されます。 セットアップ、承認、ペイロードの動作は同じです。 呼び出し元のコンテキストのみが異なります。
Note
-
Server.Connector.CloudFlow.TriggerAsyncは非同期です。awaitを使用し、呼び出し元の関数asyncをマークします。 - サーバー ロジックからフローをトリガーする前に、 フローを作成 し、 承認された Web ロールを使用してサイトに追加します。 サインインしているユーザーは、これらのロールのいずれかを保持する必要があります。
- サイトを別の環境に移動する場合は、呼び出す前に 、ターゲット環境にクラウド フローを登録 します。
TriggerAsync
クラウド フローをトリガーし、応答エンベロープを返します。
Server.Connector.CloudFlow.TriggerAsync(string flowId, string payload = null)
-
flowId: フロー識別子。 クラウド フロー API URI (/_api/cloudflow/v1.0/trigger/<guid>) に表示されるのと同じ GUID。 この値は、サイトの [クラウド フロー ] ページの [設定>Integrations] にあります。 -
payload: トリガー入力パラメーターを含む省略可能な JSON 文字列。フローのトリガーで定義されたパラメーター名によってキー指定されます。 フローが入力を受け取っていない場合は、nullまたは空の文字列を渡します。siteId、siteUrl、およびuserIdがペイロードに自動的に追加されます。
Example
async function post() {
let flowId = "00000000-0000-0000-0000-000000000001";
let payload = JSON.stringify({ Location: "Seattle" });
let response = await Server.Connector.CloudFlow.TriggerAsync(flowId, payload);
let result = JSON.parse(response);
if (!result.IsSuccessStatusCode) {
Server.Logger.Error("Cloud flow trigger failed: " + result.ReasonPhrase);
return JSON.stringify({ success: false, error: result.ReasonPhrase });
}
return JSON.stringify({ success: true, flowResponse: result.Body });
}
例: 応答
{
"StatusCode": 200,
"Body": "JsonString",
"IsSuccessStatusCode": true,
"ReasonPhrase": "OK",
"ServerError": false,
"ServerErrorMessage": null
}
クラウド フローに応答アクションが含まれていない場合は、 202 Accepted と空の Bodyが返されます。 サーバー ロジックでは、この応答は IsSuccessStatusCode: trueとして報告されます。
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 呼び出しの場合、この値はoperation タグのserverlogic パラメーターに対応します。 |
Headers |
HTTP | HTTP 要求ヘッダー。 |
HttpMethod |
HTTP | HTTP 要求メソッド ( GET、 POST、 PUT、 PATCH、 DELETEなど)。 |
Input |
Liquid |
input Liquid タグのserverlogic パラメーターを介して指定された入力文字列。 入力に構造化データが含まれている場合は、使用する前に 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 テーブルを操作する方法