Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

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.

What is UniDAC

Why choose UniDAC

Cross-platform compatibility and cross-IDE support
Multi-platform and multi-IDE support
UniDAC supports various platforms and development environments, including Delphi, C++Builder, and Lazarus. It enables development for development for Windows, macOS, Linux, iOS, and Android across x86 and x64 architectures.
Server-independent SQL
Extended SQL syntax
UniDAC supports extended SQL syntax, enabling developers to write more complex queries. This allows for greater flexibility in building powerful, data-driven applications across multiple database systems.
Access to cloud services
Cloud connectivity
UniDAC integrates with popular cloud services like Salesforce, QuickBooks, Zoho CRM, and others, allowing applications to manage data directly from cloud-based platforms.
Support for EntityDAC
Support for EntityDAC
UniDAC works with EntityDAC, allowing developers to use Object-Relational Mapping (ORM) for Delphi, with support for LINQ, making database queries more intuitive and streamlined.

Connect to Dynamics 365 from Delphi via UniDAC

Prerequisites
  1. RAD Studio installed on your system
  2. UniDAC from Devart installed
  3. Dynamics 365 connection details: server address, username, and password
  4. Devart ODBC Driver for Dynamics 365 installed

Step 1. Install and configure ODBC Driver for Dynamics 365

  1. Download the driver.
  2. Launch the downloaded installer and follow the step-by-step instructions in the setup wizard.
  3. Open ODBC Data Source Administrator and navigate to the System DSN or User DSN tab.
  4. Click Add and select ODBC Driver for Dynamics 365 from the list of available drivers. Then click Finish.
  5. 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.
  6. Optional: Click Test Connection to ensure the driver is configured correctly and can connect to Dynamics 365. Then click OK.
  7. 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.
Configure connection to Dynamics 365 - Set up ODBC Driver

Step 2. Configure connection to Dynamics 365 via UniDAC

  1. Launch your RAD Studio.
  2. Navigate to File > New > Windows VCL Application – Delphi to create a new Delphi project.
  1. 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.
  1. 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.
  2. 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:

  1. Navigate to the Component palette, locate the UniDAC category, and drag the TUniQuery and TUniDataSource components onto your form.
  2. Similarly add the TODBCUniProvider component: drag and drop it from the UniDACProviders category.
  3. Finally, add the TButton component from the Standard category and TDBGrid from the Data Controls category.
  4. Click the DBGrid1 label in the Form Designer and set the DataSource property to the UniDataSource1 value in the Object Inspector.
  5. Click the UniDataSource1 label in the Form Designer and set the DataSet property to the UniQuery1 value in the Object Inspector.
  1. 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;
      
  1. Click OK to save changes and close the dialog.
  2. 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.

  1. Add a new TUniQuery component to the Form Designer, or modify the existing one as needed.
  2. Select the UniQuery1 label in the Form Designer, and in the Object Inspector, set the Active property to True.
  3. 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}';
  1. Click Execute, then click OK to close the dialog.
  2. 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.

  1. Add the TUniQuery, TUniDataSource, and TODBCUniProvider components to your form.
  2. Click the UniDataSource1 label in the Form Designer and set the DataSet property to the UniQuery1 value in the Object Inspector.
  3. Double-click the UniQuery1 label in the Form Designer and enter a query to select the data from Dynamics 365.
  4. Click OK to save changes and close the dialog.
  5. 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!