cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Bala9677
Frequent Visitor

Unable to use Web api call in canvas app

Hi All,


I am trying to use web API(link below) call, but the below link says it is only available for Model driven apps.

https://docs.microsoft.com/en-us/powerapps/developer/component-framework/reference/webapi

Any alternate way to use API call in Canvas App? Any suggestion much appreciated.

3 REPLIES 3
ben-thompson Impactful Individual
Impactful Individual

Re: Unable to use Web api call in canvas app

You will need to create standard XMLHttpRequests. This is a sample I posted earlier today (it calls an action within a Model driven app) you will just need to change the baseUrl, query and body contents.

 

var req = new XMLHttpRequest();
		var baseUrl=this.baseUrl;
var query="/api/data/v9.1/hdn_ValidateLicense";
		return new Promise(function (resolve, reject) {

			req.open("POST", baseUrl + query, true);
			req.onreadystatechange = function () {
				
				if (req.readyState !== 4) return;
				if (req.status >= 200 && req.status < 300) {
					
					// If successful
					try {
						
						var result = JSON.parse(req.responseText);
						if (parseInt(result.StatusCode) < 0) {
							reject({
								status: result.StatusCode,
								statusText: result.StatusMessage
							});
						}
						resolve(req.responseText);
					}
					catch (error) {
						throw error;
					}

				} else {
					// If failed
					reject({
						status: req.status,
						statusText: req.statusText
					});
				}

			};
			req.setRequestHeader("OData-MaxVersion", "4.0");
			req.setRequestHeader("OData-Version", "4.0");
			req.setRequestHeader("Accept", "application/json");
			req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var body=	"{'ProductName': '"+productName+"'}";
			req.send(body);
		});

 

---
If this post has answered your question please consider it for "Accept as Solution" or if it has been helpful give it a "Thumbs Up".

Re: Unable to use Web api call in canvas app

I really hope that we will get the Xrm.WebApi implemented in Canvas Apps at some point - but it does kind of go against the principle of Canvas Apps - using connections to communicate with CDS.

@ben-thompson this has got me thinking - would this code work in Canvas - wouldn't the user need to be already authenticated with CDS first so that there is a bearer cookie? If you are running on the mobile app I'm not even sure that the bearer cookie would be available? You'd need to do an auth request to get the access token first. And then there is CORS? - https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/oauth-cross-origin-resource...

Highlighted

Re: Unable to use Web api call in canvas app

Just gave it a go and managed to call with auth using adal:

 

config = {tenant: "mytennant.onmicrosoft.com",clientId :"51f81489-12ee-4a9e-aaae-a2591f45987d"}
adalContext = new AuthenticationContext(config);
adalContext.acquireToken("https://org.crm11.dynamics.com",
(e, accesstoken) => fetch("https://org.crm11.dynamics.com/api/data/v9.1/accounts",
{headers:{"Authorization":"Bearer " + accesstoken}}).then(
  response=>response.text().then(b=>console.log(b))
));

 

 

Helpful resources

Announcements
MBAS Gallery 2020

MBAS Gallery 2020

Watch Microsoft Business Applications Summit sessions on-demand.

‘Better Together’ T-Shirt Contest – Winner Announced!

‘Better Together’ T-Shirt Contest – Winner Announced!

And the winner is...

firstImage

New Ranks and Rank Icons released on April 21!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power Apps Community!

Power Platform 2020 release wave 1 plan

Power Platform 2020 release wave 1 plan

Features releasing from April 2020 through September 2020

Users online (6,094)