How to connect to Dynamics 365 from Delphi using UniDAC
What is Dynamics 365
Dynamics 365 is a cloud-based suite of business applications developed by Microsoft that combines Customer Relationship Management (CRM) and Enterprise Resource Planning (ERP) capabilities. It offers tools for managing various business functions, including sales, customer service, marketing, finance, operations, and supply chain. By integrating with other Microsoft products like Office 365, Azure, and Power BI, Dynamics 365 provides a unified platform for data-driven decision-making, automation, and collaboration.
What is UniDAC
UniDAC (Universal Data Access Components) is a comprehensive library of Delphi and Lazarus components for database connectivity. Developed by Devart, UniDAC provides a unified interface to work with various databases, such as MySQL, SQL Server, PostgreSQL, Oracle, and SQLite. UniDAC simplifies database development by allowing applications to connect to multiple database types without changing the code. UniDAC is known for its cross-platform support, advanced features like direct mode for some databases, and compatibility with popular frameworks, making it an efficient solution for building robust and scalable database applications.
Why choose UniDAC
Connect to Dynamics 365 from Delphi via UniDAC
- RAD Studio installed on your system
- UniDAC from Devart installed
- Dynamics 365 connection details: server address, username, and password
- Devart ODBC Driver for Dynamics 365 installed
Step 1. Install and configure ODBC Driver for Dynamics 365
- Download the driver.
- Launch the downloaded installer and follow the step-by-step instructions in the setup wizard.
- Open ODBC Data Source Administrator and navigate to the System DSN or User DSN tab.
- Click Add and select ODBC Driver for Dynamics 365 from the list of available drivers. Then click Finish.
- In the dialog that opens, configure the connection settings: provide a connection name, enter the server URL, and select the appropriate authentication method. For OAuth, enter the Refresh Token from your Dynamics 365 application; otherwise, specify the username and password.
- Optional: Click Test Connection to ensure the driver is configured correctly and can connect to Dynamics 365. Then click OK.
- The ODBC Data Source for Dynamics 365 should appear in the list of User Data Sources in the ODBC Data Source Administrator. Click OK to close the dialog.
Step 2. Configure connection to Dynamics 365 via UniDAC
- Launch your RAD Studio.
- Navigate to File > New > Windows VCL Application – Delphi to create a new Delphi project.
-
Add the TUniConnection component to your form.
- 3.1 Navigate to the Component palette and locate the UniDAC category.
- 3.2 Drag the TUniConnection component and drop it onto the Form Designer.
-
Double-click the UniConnection1 label on the form to open the
configuration dialog, and provide the following TUniConnection
properties:
- In the Provider dropdown menu, select Dynamics 365.
- In the Server field, enter your Dynamics 365 URL.
- In the Username field, enter your Dynamics 365 username.
- In the Password field, enter your Dynamics 365 password.
- Click Connect.
Configure connection from the Code Editor
To configure a connection to Dynamics 365 from the Code Editor, you can use the UniDAC's TUniConnection component. First, drag and drop the component onto your form. Then, open the Code Editor and dynamically set the TUniConnection properties to establish the connection.
// Create and configure TUniConnection
UniConnection1 := TUniConnection.Create(nil);
try
UniConnection1.ProviderName := 'ODBC'; // Set provider to ODBC
UniConnection1.Server := 'Your_Dynamics_365_Server_URL';
UniConnection1.Database := ''; // Leave empty for ODBC connections
UniConnection1.Username := 'Your_Username';
UniConnection1.Password := 'Your_Password';
// Configure connection string for Dynamics 365
UniConnection1.SpecificOptions.Values['ODBC.ConnectionString'] :=
'DSN=Your_DSN_Name;'; // Use the DSN configured in the ODBC Administrator
// Attempt to connect
UniConnection1.Connect;
if UniConnection1.Connected then
ShowMessage('Connection to Dynamics 365 successful!')
else
ShowMessage('Failed to connect to Dynamics 365.');
except
on E: Exception do
ShowMessage('Error: ' + E.Message);
end;
You can place this code within a method that you call when your form is created or when a specific event is triggered.
Retrieve and manage Dynamics 365 data via UniDAC at design time
With UniDAC, you can easily connect to Dynamics 365 and access its data visually at design time. UniDAC enables developers to explore tables, run queries, and update data without writing a single line code.
Access Dynamics 365 data visually in RAD Studio
To retrieve data from Dynamics 365:
- Navigate to the Component palette, locate the UniDAC category, and drag the TUniQuery and TUniDataSource components onto your form.
- Similarly add the TODBCUniProvider component: drag and drop it from the UniDACProviders category.
- Finally, add the TButton component from the Standard category and TDBGrid from the Data Controls category.
- Click the DBGrid1 label in the Form Designer and set the DataSource property to the UniDataSource1 value in the Object Inspector.
- Click the UniDataSource1 label in the Form Designer and set the DataSet property to the UniQuery1 value in the Object Inspector.
- Double-click the UniQuery1 label in the Form Designer and enter a query to select the data from Dynamics 365. For example:
SELECT * FROM product;
- Click OK to save changes and close the dialog.
- Select the UniQuery1 label in the Form Designer, and in the Object Inspector, set the Active property to True. You will see the query result instantly.
Manipulate Dynamics 365 data visually in RAD Studio
Similarly, you can add, edit, and delete Dynamics 365 data at design time without writing any code.
- Add a new TUniQuery component to the Form Designer, or modify the existing one as needed.
- Select the UniQuery1 label in the Form Designer, and in the Object Inspector, set the Active property to True.
- Double-click the UniQuery1 label in the Form Designer and enter a query to insert, delete, or update data in the Dynamics 365 database. For example:
UPDATE product
SET description = 'A cloud-based suite of productivity tools, including Word, Excel, PowerPoint, Outlook, and Teams, designed for collaboration, communication, and efficiency.'
WHERE productid = '{a8b51bd3-f0c1-ef11-b8e9-000d3adb66be}';
- Click Execute, then click OK to close the dialog.
- Check the result.
In a similar way, you can delete or update data in Dynamics 365.
Manage Dynamics 365 data using UniDAC at runtime
Access Dynamics 365 using code
To select data from Dynamics 365 at runtime using UniDAC, a powerful library for universal data access, you can use the TUniQuery component. With UniDAC, you can write and execute SQL queries using a familiar syntax.
- Add the TUniQuery, TUniDataSource, and TODBCUniProvider components to your form.
- Click the UniDataSource1 label in the Form Designer and set the DataSet property to the UniQuery1 value in the Object Inspector.
- Double-click the UniQuery1 label in the Form Designer and enter a query to select the data from Dynamics 365.
- Click OK to save changes and close the dialog.
- Execute the query at runtime.
Example code in Delphi:
procedure TForm10.Button1Click(Sender: TObject);
begin
UniQuery1.Open
end;
Insert, update, or delete Dynamics 365 data at runtime
Insert data
To insert data into a Dynamics 365 table at runtime, use the INSERT INTO SQL statement. You can set up parameters for dynamic values.
Example code in Delphi:
procedure TForm1.InsertIntoProductTable;
begin
// Link the TUniQuery component to the connection
UniQuery1.Connection := UniConnection1;
// Write the SQL insert query
UniQuery1.SQL.Text := 'INSERT INTO product (productid, description, name, price) ' +
'VALUES (:ProductID, :Description, :Name, :Price)';
// Assign parameter values
UniQuery1.ParamByName('ProductID').AsString := '{b7c61bd3-f1d2-ef22-b8f9-000d3adb66bf}'; // Unique product ID
UniQuery1.ParamByName('Description').AsString :=
'A cloud-based suite of productivity tools, including Word, Excel, PowerPoint, Outlook, and Teams.';
UniQuery1.ParamByName('Name').AsString := 'Office 365 Suite';
UniQuery1.ParamByName('Price').AsFloat := 99.99;
try
// Execute the query
UniQuery1.Execute;
ShowMessage('Product inserted successfully!');
except
on E: Exception do
ShowMessage('Failed to insert product: ' + E.Message);
end;
end;
Update data
To modify existing records, use the UPDATE SQL statement with a WHERE clause to target specific rows.
Example code in Delphi:
procedure TForm1.UpdateProductDescription;
begin
// Link the TUniQuery component to the connection
UniQuery1.Connection := UniConnection1;
// Write the SQL update query
UniQuery1.SQL.Text := 'UPDATE product ' +
'SET description = :Description ' +
'WHERE productid = :ProductID';
// Assign parameter values
UniQuery1.ParamByName('Description').AsString :=
'A cloud-based suite of productivity tools, including Word, Excel, PowerPoint, Outlook, and Teams, designed for collaboration, communication, and efficiency.';
UniQuery1.ParamByName('ProductID').AsString := '{a8b51bd3-f0c1-ef11-b8e9-000d3adb66be}';
try
// Execute the query
UniQuery1.Execute;
ShowMessage('Product description updated successfully!');
except
on E: Exception do
ShowMessage('Failed to update product description: ' + E.Message);
end;
end;
Delete data
To delete records, use the DELETE FROM SQL statement with a WHERE clause to specify which rows to remove.
Example code in Delphi:
procedure TForm1.DeleteProduct;
begin
// Link the TUniQuery component to the connection
UniQuery1.Connection := UniConnection1;
// Write the SQL delete query
UniQuery1.SQL.Text := 'DELETE FROM product WHERE productid = :ProductID';
// Assign the parameter value
UniQuery1.ParamByName('ProductID').AsString := '{aab51bd3-f0c1-ef11-b8e9-000d3adb66be}';
try
// Execute the query
UniQuery1.Execute;
ShowMessage('Product deleted successfully!');
except
on E: Exception do
ShowMessage('Failed to delete product: ' + E.Message);
end;
end;
Dynamics 365-specific options
UniDAC provides a unified interface for working with various cloud services, including Dynamics 365, while allowing fine-tuned configuration for specific clouds. These configurations, known as Dynamics 365-specific options, enable you to optimize connectivity and behavior. These options can be applied to components like TUniConnection, TUniQuery, TUniTable, TUniStoredProc, and TUniSQL through the SpecificOptions property, which is a string list.
How to use Dynamics 365-specific options
UniConnection1.SpecificOptions.Values['Authentication'] := 'atOAuth';
UniQuery1.SpecificOptions.Values['FetchAll'] := 'True';
Benefits of Dynamics 365-specific options in UniDAC
UniDAC's Dynamics 365-specific options offer significant advantages, simplifying configuration, improving performance, and ensuring flexibility. Features like customizable authentication (OAuth 2.0 or basic), metadata caching, and proxy settings enable seamless integration with Dynamics 365, even in complex enterprise environments.
Options such as FetchAll, FieldsAsString, and UseUnicode optimize data handling, guaranteeing smooth operations across diverse datasets and multilingual systems.
Performance-focused settings like CommandTimeout and ConnectionTimeout improve stability during long-running operations, while UTCDates ensures accurate datetime values across time zones. These enables developers to build secure, efficient, and scalable applications tailored to modern business requirements.
Conclusion
In conclusion, connecting to Dynamics 365 from Delphi using UniDAC is an easy and straightforward process. Whether you need to retrieve, update, or insert data, UniDAC streamlines the integration, allowing you to focus on creating feature-rich applications. Experience the power of UniDAC for Dynamics 365 by downloading a free trial today and unlocking its full potential in your projects. Build smarter, faster, and more efficient Delphi applications with UniDAC!