| Muhammad Uzair

Use WebAPI in the PowerApp Portals
The following code will allow us to use the webAPI and fetch
all the contact from the dataverse and display in the div tag with id of contactList.
$(function () {
//Web API ajax wrapper
(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)
$(document).ready(async function () {
//Get all the contact
await webapi.safeAjax({
type: "GET",
url: "/_api/contact?$filter=statecode eq 0 and isinactive eq false",
contentType: "application/json",
success: async function (res) {
if (res !== null && res.value.length > 0) {
var listofItems = res.value;
for(var listofContact in listofItems){
var conatactName = res.value[listofContact].name;
var el = document.createElement("option");
el.textContent = CarrierName;
el.value = conatactName;
$('#ContactList').append(el);
}
}
else{
//alert("No carrier found");
}
},
error: function (error) {
alert("Error:" + error);
}
});
});
});
Join us next time, as we continue our journey of learning canvas apps.Click here to learn more about Imperium's Power Apps Services. We hope this information was useful, and we look forward to sharing more insights into the Power Platform world.