web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Unable to use Web api ...
Power Apps
Unanswered

Unable to use Web api call in canvas app

(0) ShareShare
ReportReport
Posted on by 31

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.

I have the same question (0)
  • Ben Thompson Profile Picture
    1,400 on at

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

     

  • ScottDurow Profile Picture
    1,039 on at

    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-sharing-connect-single-page-application

  • ScottDurow Profile Picture
    1,039 on at

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

     

     

  • cchannon Profile Picture
    4,702 Moderator on at

    I wrestled with this same issue in a separate thread ( pcf assign output params for canvas app operations ) . Rather than using adal, I decided to push the database operations back up to PowerApps. I could say I did it because I wasn't certain the token would be available without MFA, but really... it's just because I'm lazy.

     

    Within your Canvas app, what you need to do is define one or more Output type params, then give it the data you need to undertake your action (e.g. for an update, the GUID and attributes to update) then you call notifyOutputChanged to let the canvas app know your output is ready.  Back up in your Canvas App, you just put your logic into the OnChange event on the control (which is essentially what you are calling with notifyOutputChanged). Then, your outputs should be available in <controlname>.<outputparamname> so you can do your CDS operations from up here or even in Flow.

     

    Quick note though: a big drawback you're going to run into with this approach is the known bug @HemantG and team are working on where a canvas app caches its embedded pcf and doesn't refresh the control when you update pcf in solution. Because you will be splitting your logic across the pcf and the canvas app, you will likely need to iterate a few times and will need to delete the canvas app and recreate it every time you make a change in pcf. Definitely a big headache, but they are working on a fix and I'm sure it will be available soon.

  • Hemant Gaur Profile Picture
    on at

    For now best solution is to use the output and let canvas app store the data. I have used patch in one of my recent exploration to save data to CDS. PCF upgrade issue should be fixed now and you should see PCF update prompt when the app is reopened. We will have more tighter inner loop in future releases for canvas apps. 

     

    hemant

  • BulbMatt Profile Picture
    2 on at

    Scott, Do you have any other details about how you got this to work? I have the need to load data from CDS in a grid that is aggregated and I can't seem to figure out any other way to do it than this. I did look at what @cchannon and @HemantG provided, but I just don't think I can get the data in Power Apps.

     

    I have tried using msal and adal but in both cases I get to a point where I get the following from the authorize endpoint

    AADSTS50050: The request is malformed: invalid format for 'authorization_endpoint' value.  

     and a bunch of calls to "https://pa-content.azureedge.net/resource/webplayerdynamic/publishedapp/preloadindex" with a 500 error of 

    Returning InternalServerError for resource publishedapp/preloadindex after trying all 1 configured resource origins.

    Any help you could provide would be greatly appreciated. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard