Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Connect Visual Studio to Dynamics 365

Posted on by 340

Hello,

Can someone help me, comment on, provide sample codes or links on how I can connect visual studio to dynamics 365 for Javascript or C# ? 


I want to connect visual studio to dynamics 365 to create Javascript or C# apps.

thanks 

  • Suggested answer
    CRMJetty Profile Picture
    CRMJetty 3,508 on at
    RE: Connect Visual Studio to Dynamics 365

    Hello Johnseito,

    Microsoft Dynamics 365 development you may be wondering how to  connect to it via a .Net C# console application.
    1.Open the visual studio and select the file.
    2.Click on visual c# and select the console app(.Net Framework).
    3.Enter a project name within the name field and click ok.


    The next step is to add the Dynamics 365 SDK references to the project and  this is quite easy now it’s managed via NuGet.


    4.Select Tools | NuGet Package Manager | Manage NuGet Packages for Solution
    5.This screen defaults to showing the installed packages, click Browse
    6.In the Search textbox type: Dynamics 365 XRMTooling and select Microsoft.CrmSdk.XrmTooling.CoreAssembly
    7.Select the Project in the right-hand side window and click install.
    8.We need to add one more package, search for Microsoft.CrmSdk.


    CoreAssemblies
    under the browse tab and follow the same process to add it to the  project too.


    9.Add the following namespace in the program.cs file.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Crm.Sdk.Messages;
    using System.Net;
    using System.ServiceModel.Description;


    10. Add the following code underneath to check the connection by attempting to retrieve the current user ID:

    if(oServiceProxy !=null)
    { Guid userid =((WhomAmResponse)oServiceproxy.Execute
    (new WhomAmResponse()) ).
    UserId;
    if(userid !=Guid.Empty){
    Console.Writeline(“Connection Successful”);}
    }
    else {
    Console.Writeline(“Connection failed”);
    }


    And You are done Start button, run the program and if the connection details are correct, you should see “Connection Successful!”:

  • Suggested answer
    RE: Connect Visual Studio to Dynamics 365

    Hi

    As far as I know, you should be able to use the same connection,

    Additionally you can refer if that helps community.dynamics.com/.../549227

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Connect Visual Studio to Dynamics 365

    Thanks,

    How about visual studio coding client side javascript ?

    Is this the same connection or I would need to do something else ?

  • Suggested answer
    RE: Connect Visual Studio to Dynamics 365

    Hi Partner,

    You can refer to link https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/286468/how-to-connect-dynamics365-with-visual-studio/820439 

    Which might be helpful here

    Additionally you can also refer https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/visual-studio-dot-net-framework

    You can also try following the mentioned steps:

    Step 1: Open Visual Studio and Click on New Project. Click on Visual C# and select Console Application.

    Give Name to the Project and click on OK.

    pastedimage1593323728363v1.png

    Step 2: Goto Visual Studio Tools -> NuGet Package Manager and click on Package Manger Console.

    pastedimage1593323728363v2.png

    Step 3: In Package Manager Console, Copy & Paste the below command to get the required CRM SDK Dlls and press Enter.

    Click here to know the Latest version of the CRM Dlls.

    Install-Package Microsoft.CrmSdk.CoreTools -Version 9.0.0.7

    pastedimage1593323728363v3.png

    Step 4: Latest CRM SDK Dlls are installed in the project’s bin\coretools folder. You can also see the success message in Package Manager Console.

    pastedimage1593323728363v4.png

    Step 5: Right Click on References and navigate to bin\coretools folder.

    Add the below Dlls to the Visual Studio project.

    Microsoft.Crm.Sdk.Proxy.dll

    Microsoft.Xrm.Sdk.dll

    pastedimage1593323728364v5.png

    And also add the below and click on OK,

    System.Runtime.Serialization

    System.ServiceModel

    pastedimage1593323728364v6.png

    Step 6: Add the below namespaces.

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Crm.Sdk.Messages;
    using System.Net;
    using System.ServiceModel.Description;

    pastedimage1593323728364v7.png

    Step 7: Copy and Paste the below Code in Main method.

    IOrganizationService organizationService = null;
    
    try
     {
     ClientCredentials clientCredentials = new ClientCredentials();
     clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>.onmicrosoft.com";
     clientCredentials.UserName.Password = "<ProvideYourPassword>";
    
    // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    // Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
    // Copy and Paste Organization Service Endpoint Address URL
    organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<ProvideYourOrgName>.api.<CRMRegion>.dynamics.com/XRMServices/2011/Organization.svc"),
     null, clientCredentials, null);
    
    if (organizationService != null)
     {
     Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
    
    if (userid != Guid.Empty)
     {
     Console.WriteLine("Connection Established Successfully...");
     }
     }
     else
     {
     Console.WriteLine("Failed to Established Connection!!!");
     }
     }
     catch (Exception ex)
     {
     Console.WriteLine("Exception caught - " + ex.Message); 
     }
     Console.ReadKey();

    Step 8: Right click on the solution and build it. If build is succeeded, Run the application to see the below Message.

    pastedimage1593323728364v8.png

    Note:

    Problem: If you face any build issues like the below, while building the project, then use the below solution and resolve it.

    pastedimage1593323728365v9.png

    Solution: Follow the below steps to resolve the problem.

    1. Change the Project’s Dot Net Framework from 4.5 to 4.5.2, this change is required because latest CRM SDK Dlls needs the 4.5.2 version of Dot Net Framework.

    pastedimage1593323728365v10.png

    2. Click on Yes and build the project.

    pastedimage1593323728365v11.png

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans