Portal (Power Pages) WebAPI & forget about clearing cache


Portals WebAPI is another way to execute CRUD actions on Dataverse from Power Pages and is mainly used for customizations. However, this is not configured by default.

How do you set it?
Go to Portal Management > Website > Site Settings
Create the following new site-setting records per table
  1. "Webapi/[table name]/enabled"  where the value is true 
  2. "Webapi/[table name]/fields"   where the values are the fields used, separate these by a comma or asterisk * to select all fields.
  3. "Webapi/[table name]/disableodatafilter"  where the value is true or false depending on the requirement
  • Webapi/error/innererror" where value is true

Security

The good news is that all restrictions on your transactions depend on what user is signed and which web roles have been assigned because they rely on your Table Permissions configuration.



Actions Allowed:
  • GET
  • PATCH
  • PUT
  • DELETE
  • POST

For example:

function Update(_id) {
var record = {};
record.[fieldname] =[value]; // Boolean

webapi.safeAjax({
type: "PATCH", // GET/PUT/DELETE/POST
contentType: "application/json",
url: `/_api/[table](${[_id]})`,
data: JSON.stringify(record),
success: function (data, textStatus, xhr)
        {
console.log("Success message");
},
error: function (xhr, textStatus,
                        errorThrown)
        {
console.log(xhr);
}
});
}

if you want to validate your request or create one with an interface, you can use "Dataverse Rest Builder" in XRMTool, once you created your request go to Portal tab




Nevertheless, you can only CUD (Create, Update, Delete) up to 1 record in a transaction. But you can use a Promise with jQuery/JavaScript for multiple requests

let requests = new Array()
let records = new Array()

records.forEach( record => {
requests.push( SaveRecords(record))
});

Promise.all(requests).then(function(){
alert("Success")
})


function PatchRequest(record){
const urlAPI = `/_api/[table](${record.id})`
return webapi.safeAjax({
type: "PATCH",
contentType: "application/json",
url: urlAPI,
data: JSON.stringify(record),
success: function (data, textStatus, xhr)
        {
console.log('Success')
},
error: function (xhr, textStatus,
                        errorThrown)
        {
console.log('Error');
}
});
}


Microsoft provides The following function and is required to use the API. You can find it as well on the documentation. 

(function(webapi, $){
function safeAjax(ajaxOptions) {
var deferredAjax = $.Deferred();

shell.getTokenDeferred().done(
        function (token) {
// add headers for ajax
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
"__RequestVerificationToken": token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(
                function(data, textStatus, jqXHR)
                {
validateLoginSession(data, textStatus,
                  jqXHR, deferredAjax.resolve);
}).fail(deferredAjax.reject); //ajax
}).fail(function () {
deferredAjax.rejectWith(this, arguments);
         // on token failure, pass the token ajax and args
});

return deferredAjax.promise();
}
webapi.safeAjax = safeAjax;
})(window.webapi = window.webapi || {}, jQuery);


Interesting Tip

Sometimes, when you customize PowerPages, there appears to be a cache issue that can be inconvenient for the UX. I have seen this in multiple projects. But you can forget about this issue if you use Liquid + WebAPI.

Comments

Popular posts from this blog

Data Migration Toolbox

Using external Fonts in Power Pages