伺服器邏輯在伺服器命名空間下提供內建物件。 這些物件可讓您記錄訊息、呼叫外部服務、使用 Dataverse 或存取要求詳細資料,從而簡化開發。
HttpClient
使用 HTTP 用戶端,透過傳送 HTTP 請求來與外部服務整合。
Note
目前,伺服器邏輯僅application/jsontext/htmlapplication/x-www-form-urlencoded支援請求主體中的內容類型、。
Examples
HTTP 取得
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 帖子
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 放
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 修補程式
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"
}
}
網站設定
使用此連接器讀取目前網站的網站設定值。
Note
不要直接將 API 金鑰或憑證等秘密儲存在伺服器邏輯中。 相反地,請將它們安全地存放在 Azure Key Vault,透過環境變數取得資料來源,並透過網站設定來參考。
Example
Server.SiteSetting.Get("Search/Enabled");
EnvironmentVariable
使用此連接器讀取環境變數的值。
Example
Server.EnvironmentVariable.get("SITEPATH");
網站
使用此連接器取得 Dataverse 中目前網站紀錄的詳細資訊。
Example
Server.Website.adx_primarydomain;
使用者
提供已登入使用者的詳細資料。 如果匿名,則傳回 null。
Example
Server.User.fullname;
Dataverse
使用物件 Server.Connector.Dataverse 對 Dataverse 資料表執行 CRUD 操作並呼叫自訂 API。
Note
- 當你在程式碼中參考 Dataverse 資料表時,請使用 EntitySetName。 例如,要存取該
account資料表,請使用 EntitySetNameaccounts。
建立記錄
建立新記錄。
Server.Connector.Dataverse.CreateRecord(string entitySetName, string payload)
範例
Server.Connector.Dataverse.CreateRecord("accounts", "{\"name\": \"Contoso Ltd.\", \"telephone1\": \"555-555-0100\", \"websiteurl\": \"https://contoso.com\"}");
檢索記錄
依 ID 擷取單一記錄。
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");
擷取多筆記錄
擷取記錄集合。
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");
更新記錄
依 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\" }");
刪除記錄
依 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"
}
}
雲流
用這個Server.Connector.CloudFlow物件從伺服器邏輯觸發 Power Automate 雲端流程。 流程必須已經 加入你的網站。 這就像伺服器端用 網頁的雲端流程 API 呼叫流程一樣。 它使用與 /_api/cloudflow/v1.0/trigger/<guid> 端點相同的底層管線。 設定、授權和有效載荷行為都是一樣的。 只有通話的上下文不同。
Note
-
Server.Connector.CloudFlow.TriggerAsync是非同步的。 使用await並標記呼叫函式async。 - 在你從伺服器邏輯觸發流程之前,先 建立流程 並 以授權的網頁角色加入你的網站。 登入的使用者必須持有其中一個角色。
- 如果你將一個站點移到另一個環境,請先 在目標環境中註冊雲端流 ,再呼叫它。
觸發非同步
觸發雲流並回傳響應包絡線。
Server.Connector.CloudFlow.TriggerAsync(string flowId, string payload = null)
-
flowId: 流量識別碼。 與雲端流程 API 中的 URI/_api/cloudflow/v1.0/trigger/<guid>() 相同 GUID。 你可以在網站的 雲端流程 頁面「 設定>整合」中找到這個數值。 -
payload: 可選的 JSON 字串,包含 觸發器輸入參數,並以流程觸發器上定義的參數名稱來鍵結。 如果流程沒有輸入,就通過null或空字串。siteId這些 、siteUrl、userId會自動加入有效載荷。
範例
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");
背景
物件提供 Server.Context 目前伺服器邏輯調用的資訊。 可用的屬性取決於伺服器邏輯是透過 HTTP 請求呼叫,還是從 Liquid 範本呼叫。
屬性
| 名稱 | 可使用於 | 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 請求的完整網址。 |
存取 HTTP 請求上下文
以下範例在 id 透過 HTTP 請求呼叫伺服器邏輯時讀取查詢參數:
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
});
}
存取液體呼叫上下文
當從 Liquid 呼叫伺服器邏輯時,請用 Server.Context.Input 來讀取透過標籤 input 參數提供的值。
例如,以下 Liquid 會傳遞 JSON 輸入:
{% assign inputData = '{"category":"active","maximumResults":5}' %}
{% serverlogic output: result, name: 'customer-summary', operation: 'getSummary', input: inputData %}
伺服器邏輯操作可以解析輸入並存取有關液體調用的資訊:
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
});
}