Skip to content
← All tips

Create and update records from Power Pages with the Web API

Once the Web API is enabled for the table and the safeAjax wrapper is in the Header, creating a record is a POST and updating one is a PATCH, both against /_api/<entity-set>:

function createRecord(entitySet, data) {
  return utilities.safeAjax({
    type: "POST",
    url: "/_api/" + entitySet,
    contentType: "application/json",
    data: JSON.stringify(data),
    success: function (res, status, xhr) {
      console.log("Created with id", xhr.getResponseHeader("entityid"));
    },
  });
}
 
function updateRecord(entitySet, id, data) {
  return utilities.safeAjax({
    type: "PATCH",
    url: "/_api/" + entitySet + "(" + id + ")",
    contentType: "application/json",
    data: JSON.stringify(data),
  });
}
 
createRecord("accounts", { name: "Axazure", address1_city: "Madrid" });

Note that the name is plural, since it’s the entity set (accounts, contacts). And you won’t find the new record’s id in the response body; it comes back in the entityid header.

If you try to read what you just created straight away, it may not be there yet. That’s the Power Pages cache, not a bug in your code.

Did it help?

If you need help with this in your project, write to me.

More tips