Edit

Share via


Author server logic

Create server logic in the Set up workspace in design studio. Each server logic record represents a distinct server API that you can invoke from the client with supported HTTP verbs.

Steps to create server logic

  1. Sign into Power Pages.
  2. Select site + Edit.
  3. Navigate to the Set up workspace, then select Server logic (preview). Server logic preview screen showing different operations
  4. Select +New server logic.
  5. Enter name for the server logic. This name is used in API as resource identifier while constructing the server logic API.
  6. Select +Add roles to assign appropriate web role.
  7. Select 3 dots () next to name and select Edit code.
  8. Select Open Visual Studio Code to author the custom logic.

Call server logic from client script

Note

Each request to server logic request should include Cross-Site Request Forgery (CSRF) token. In general, shell.safeAjax method wraps the CSRF token. Learn more about how to construct requests in 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
}

Next step

Server objects

Server logic overview
How to interact with Dataverse tables using server logic