Skip to main content

Notifications

Announcements

No record found.

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

Plugin to connect to another enviornment

Posted on by 125

Hi All,

We need to make connection to other enviornment so that we can use it to retrieve data using plugin but we cannot figure out how to make connection to other enviornment .

Microsoft is using https://learn.microsoft.com/en-us/power-apps/developer/data-platform/virtual-entities/sample-ve-provider-crud-operations 

to make connection to external SQL table but we want to connect to other enviornment in same tennant to leverage the data. 

This is the code to connect to SQL 

public static class Connection { public static SqlConnection GetConnection()

{ try

{ //sample database to connect to SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

builder.DataSource = "Enter name or network address of the SQL Server";

builder.UserID = "Enter User Name"; builder.Password = "Enter password"; builder.InitialCatalog = "Enter database details";

SqlConnection connection = new SqlConnection(builder.ConnectionString);

return connection; } catch (SqlException e) { Console.WriteLine(e.ToString()); throw; }

} }

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Plugin to connect to another enviornment

    Hi,

    Please see below article to understand how we can connect to other crm instance from plugin

    www.google.com/.../

  • Dynamics_User07 Profile Picture
    Dynamics_User07 125 on at
    RE: Plugin to connect to another enviornment

    Hi Ganesh,

    This Solution is legit but client cannot allow us to use any Azure webs services so did not consider it..not sure if there is any other way

  • Verified answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Plugin to connect to another enviornment

    Hi,

    To connect to another environment within the same tenant in Dynamics 365, you can use the Dynamics 365 Web API. The Web API is a RESTful API that allows you to interact with Dynamics 365 data using standard HTTP requests.

    Here is an example of how you can use the Web API to connect to another environment:

    1. Register an app in Azure Active Directory (AAD) and grant it the necessary permissions to access the other environment's data.

    2. Acquire an access token using the app's client ID and secret.

    3. Use the access token to authenticate your requests to the other environment's Web API.

    4. Use the Web API to retrieve the data you need using standard HTTP requests (GET, POST, PUT, DELETE).

    Here is an example of how you can use the Web API to retrieve data from another environment:

    string resource = "https://your-other-environment.crm.dynamics.com";
    string clientId = "your-client-id";
    string clientSecret = "your-client-secret";
    string accessToken;
    
    using (var client = new HttpClient())
    {
        var request = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/your-tenant-id/oauth2/token");
        request.Content = new FormUrlEncodedContent(new Dictionary
        {
            { "resource", resource },
            { "client_id", clientId },
            { "client_secret", clientSecret },
            { "grant_type", "client_credentials" }
        });
    
        var response = await client.SendAsync(request);
        var json = await response.Content.ReadAsStringAsync();
        var token = JsonConvert.DeserializeObject<Dictionary>(json);
        accessToken = token["access_token"];
    }
    
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var response = await client.GetAsync($"{resource}/api/data/v9.1/accounts");
        var json = await response.Content.ReadAsStringAsync();
        var accounts = JsonConvert.DeserializeObject<List>(json);
    }
    

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans