WHY IS THIS NECESSARY
- Data Migration : To import data into Dynamics 365 from external sources / export data from Dynamics 365 to external system.
- Automate business processes.
- Platform Integrations.
- Running scheduled jobs / background tasks that interact with Dynamics 365 without user interaction.
- To perform CRUD operations programmatically. Creating / Read /Update /Delete records can be done.
PRE-REQUISITES
- Install visual studio
- Install CRM SDK NuGet packages.
Steps
Step 1:
Step 1:
- Open Visual Studio and create a new project.
- Select Console App(.NET Framework) c# and give the project name and location (where to save the project).
- Go to solution explorer , Right click the solution and select the Manage NuGet packages for solution.
- Search and Install packages “Microsoft.Xrm.Tooling” and “Microsoft.CrmSdk.XrmTooling.CoreAssembly”.
- Open App.config file.
- Add the app settings details.
Replace the UserName, password and Environment placeholders with your Dynamics 365 CRM UserName, Password and organization name respectively.
We can get the organisation name by clicking the Customization -> Developer Resources -> OrganizationService
- Open the Program.cs file.
- Add the following lines to make a connection with Dynamics 365.
- Once the connection is established, we can access Dynamics 365 data with the service instance.
- To create a contact record, we need to add the code
Explanation:
- Create an instance of the entity contact.
- Add the field values by contact[fieldname] = value;
- Use service.create to create contact record in Dynamics 365.
- Once done. Run the code by clicking start.
- Add the code
Console.ReadKey();
This will show the output and wait for the user to enter else we can not see the result. We need to go the result window and check.
- Use try-catch to catch any issues that may occur during the connection or data retrieval process.
- To retrieve the contact data
2. To update data
The following is the full code for create, retrieve and update records.
Limitations:
- This method of connecting Dynamics365 using user name and password is suitable for test environments and admin accounts.
- It is recommended that to use OAuth2 approach with Azure ActiveDirectory.
- This method will not work if MFA setting was set for the user.