Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archive)
Answered

Plugin to connect to another enviornment

(0) ShareShare
ReportReport
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
    28,958 Moderator 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
    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
    3,392 Moderator 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

Community Spotlight of the Month

Kudos to Mohamed Amine Mahmoudi!

Blog subscriptions now enabled!

Follow your favorite blogs

TechTalk: How Dataverse and Microsoft Fabric powers ...

Explore the latest advancements in data export and integration within ...

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans