Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

API calling from C# Using Connection

(0) ShareShare
ReportReport
Posted on by 2
Hello everyone,
I'm relatively new to the Dynamics world and currently facing a challenge. I've created a new custom connector with OAuth 2.0 and successfully added some connections to it. Now, my goal is to execute an action of the custom connector using C# code. Is this possible, and if so, how can I achieve it?
After successfully calling the API and obtaining the response, my next step is to send this response to a PowerApps Component Framework (PCF) component to display the data. Any guidance or assistance on how to proceed would be greatly appreciated.
Thank you!
  • Verified answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,480 Super User 2024 Season 2 on at
    API calling from C# Using Connection
    Hi ,

    Yes you can call the custom connector using C# as shown in below code snippet:
     
    
        static async Task CallConnector(string accessToken)
        {
            // Construct HTTP request
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("https://URLofyourconnector/action"),
                Headers =
                {
                    { "Authorization", $"Bearer {accessToken}" },
                    { "Accept", "application/json" },
                    // Add other required headers if needed
                },
            };
    
            var response = await httpClient.SendAsync(request);
            if (response.IsSuccessStatusCode)
            {
                // Process successful response
                var responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"Response: {responseBody}");
            }
            else
            {
                Console.WriteLine($"Error: {response.StatusCode}");
            }
        }
    Now, to pass this response to PCF control, You need to setup a bound parameter and from C# code set that bound parameter. But PCF Control need to refresh manually to get the record.
     
    But I would suggest if you want to use response in PCF control then directly call the action inside PCF control.
     
    Thank You,
    Pradeep Rai.
     
    Please mark as Verified, if my suggestion help you to resolve the query.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,759 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans