Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Create server logic in the Set up workspace in design studio. Each server logic record represents distinct server-side logic that you can invoke from a client script by using supported HTTP verbs or from a Liquid template during server-side page rendering.
Steps to create server logic
- Sign in to Power Pages.
- Select site + Edit.
- Go to the Set up workspace, and then select Server logic.
- Select +New server logic.
- Enter a name for the server logic. The API uses this name as the resource identifier when constructing the server logic API.
- Select +Add roles to assign the appropriate web role.
- Select the three dots (…) next to the name and select Edit code.
- Select Open Visual Studio Code to author the custom logic.
Call server logic from client script
Note
Each request to server logic should include a Cross-Site Request Forgery (CSRF) token. In general, the shell.safeAjax method wraps the CSRF token. For more information about how to construct requests, see CSRF wrapper AJAX function.
Example: calling HTTP GET
shell.safeAjax({
type: "GET",
url: "/_api/serverlogics/serverlogicname?id=1",
contentType: "application/json",
success: function (res) {
console.log(res);
}
});
Example: calling HTTP POST
shell.safeAjax({
type: "POST",
url: "/_api/serverlogics/serverlogicname",
contentType: "application/json",
data: JSON.stringify({
"name": "Sample name"
}),
success: function (res) {
console.log("res");
}
});
Example: calling HTTP PUT
shell.safeAjax({
type: "PUT",
url: "/_api/serverlogics/serverlogicname",
contentType: "application/json",
data: JSON.stringify({
"name": "Sample name"
}),
success: function (res) {
console.log("res");
}
});
Example: calling HTTP PATCH
shell.safeAjax({
type: "PATCH",
url: "/_api/serverlogics/serverlogicname",
contentType: "application/json",
data: JSON.stringify({
"name": "Sample name"
}),
success: function (res) {
console.log("res");
}
});
Example: calling HTTP DELETE
shell.safeAjax({
type: "DELETE",
url: "/_api/serverlogics/serverlogicname?id=1",
contentType: "application/json"
success: function (res) {
console.log("res");
}
});
Example: Response
{
"RequestId": "811b363e-36d2-4213-9f44-a73bf7550ce8",
"Success": true,
"Data": "Sample data",
"ExecutionTime": 9256.331,
"ServerLogicName": "serverlogicname",
"Error": null
}
Call server logic from Liquid
Use the serverlogic Liquid tag to invoke server logic while a page or web template is rendered. This server-side invocation doesn't require a client-side HTTP request or Cross-Site Request Forgery (CSRF) token.
Assign the server logic record to a web role that the current website user can access.
For the complete syntax, input parameters, and returned attributes, see the serverlogic Liquid object.
Invoke server logic without input
Create a function in the server logic record.
function getSummary() {
return JSON.stringify({
message: "Server logic invoked successfully",
count: 42
});
}
Invoke the function from a page or web template:
{% serverlogic name: 'customer-summary', operation: 'getSummary', output: result %}
{% if result.success %}
<p>{{ result.data.message | escape }}</p>
<p>Count: {{ result.data.count }}</p>
{% else %}
<p>The customer summary couldn't be retrieved.</p>
{% endif %}
In this example:
customer-summaryis the name of the server logic record.getSummaryis the JavaScript function to invoke.resultis the Liquid variable containing the operation result.result.datacontains the parsed JSON returned bygetSummary.
Pass input to server logic
Use the optional input parameter to pass a string to the server logic operation. Use JSON when you need to pass structured data.
{% assign inputData = '{"category":"active","maximumResults":5}' %}
{% serverlogic name: 'customer-summary', operation: 'getSummary', input: inputData, output: result %}
{% if result.success %}
<p>Category: {{ result.data.category | escape }}</p>
<p>Maximum results: {{ result.data.maximumResults }}</p>
{% endif %}
Read the input in the server logic function through Server.Context.Input:
function getSummary() {
var input = JSON.parse(Server.Context.Input || "{}");
return JSON.stringify({
message: "Summary retrieved",
category: input.category,
maximumResults: input.maximumResults
});
}
Validate input before using it in Dataverse operations or passing it to an external service.
Note
Server logic invoked from Liquid runs while the page is rendered. Long-running Dataverse operations or external HTTP requests increase the page response time.
Limitations
The following limitations apply when authoring server logic:
Browser-specific APIs aren't supported
Server logic runs in a server environment and doesn't support browser-based APIs or libraries.
Examples includefetch,XMLHttpRequest, and other DOM-related features.Restricted keywords and patterns
To maintain a secure execution environment, the system validates server logic scripts and rejects those that contain certain unsafe or restricted keywords. These keywords include those that enable dynamic code execution, process control, or prototype manipulation.The following patterns aren't allowed:
__dirname __filename import(, import from eval(, Function( setTimeout(, setInterval(, setImmediate( process.exit, process.kill, child_process fs., require( constructor.constructor, this.constructor, arguments.callee with(, delete Object.getPrototypeOf, Object.setPrototypeOf Proxy(, Reflect. Symbol.for __proto__, prototype debugger
Next step
Related information
Server logic overview
How to interact with Dataverse tables using server logic