| 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;



                            .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;





                            //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.

About The Blog

Stay updated with what is happening in the Microsoft Business Applications world and initiatives Imperium is taking to ease digital transformation for customers.

More About Us

We provide guidance and strategic oversight to C-Suite and IT Directors for on-going implementations. Feel free to give us a call.

1 331 250 27 17
Send A Message

Ready to Start?

Get a personalized consultation for your project.

Book a Meeting