How To Bulk Import IoT Devices In Azure IoT Hub Using C#

How To Bulk Import IoT Devices In Azure IoT Hub Using C# 

In this article, we are going to see how to bulk import IoT devices in Microsoft Azure IoT Hub. Azure IoT Hub is used to connect, manage and monitor billions of IoT devices . It securely connects the devices to develop IoT applications. IoT Hub is cloud gateway which supports open source SDK’s and multiple protocols. 

Please read the previous parts of the article before continuing with this one. 

  1. How To Create Azure IoT Hub Using PowerShell 
  2. How to Register IoT Device in Azure IoT Hub Using PowerShell 
  3. How To Send Telemetry From An IoT Device To The Azure IoT Hub Using C#
  4.  How To Configure Message Routing With IoT Hub – Part One 
  5. How To Configure Message Routing With IoT Hub – Part Two 
  6. How To Setup Diagnostic Logs With An IoT Hub 

Creating a C# Console Application 

  • Open Visual Studio -> Choose Create a new project. 
  • Select Console App (.Net Core) and click Next. 
  • Enter the project name, location and solution name .  
  • Click Create. 

Installing the Microsoft.Azure.Devices NuGet package 

  • Go to Project -> Manage NuGet Packages. 
  • Click Browse tab and search for Microsoft.Azure.Devices. You will see the Microsoft.Azure.Devices SDK will have listed in the search result and click Install button 
  • Now, click the I Accept button to accept the license agreement. 
  • It will take a few minutes to install the SDK in our project 

Get IoT Hub Connection string from Azure IoT Hub 

  • Open your Azure portal and choose your IoT Hub. 
  • Click Shared access policies In Setting. You can see the list of policies to the IoT Hub. 
  • Double click the iothubowner policy and copy the connection string from the Shared access key. 
  • Here i used RegistryManager class .It contains methods that services can use to perform create , update and delete operation on devices. 
  • Here is used AddDeviceAsync() method to create the bulk IoT device. 
  •  Open the program.cs file and type the below code 

using System; 

using System.Threading.Tasks; 

using Microsoft.Azure.Devices; 

using Microsoft.Azure.Devices.Common.Exceptions; 

namespace IoTDeviceBulkImpExp 


    class Program 


        private static RegistryManager registryManager; 

        private static string connectionString = “Enter the IoT Hub Connection String”; 

        static async Task Main(string[] args) 


            registryManager = RegistryManager.CreateFromConnectionString(connectionString); 

            string deviceId = “device”; 

            for (int i = 0; i < 10; i++) 


                string newDeviceId =  deviceId + i; 

                await AddDeviceAsync(newDeviceId); 



        private async static Task AddDeviceAsync(string deviceId) 


            //string deviceId = “DeviceThree”; 

            Device device; 



                Console.WriteLine(“New device:”); 

                var d = new Device(deviceId); 

                device = await registryManager.AddDeviceAsync(d); 


            catch (DeviceAlreadyExistsException) 


                Console.WriteLine(“Already existing device:”); 

                device = await registryManager. 



            Console.WriteLine(“Generated device key: {0}”, 





  • Run the project using F5 function key . 
  • Now go to the Azure Portal and check the new IoT devices are created. 

That’s it I hope you have learnt how to bulk import the IoT devices in Azure IoT Hub  programmatically using C#. Feel free to fill up the comment box below, if you need any further assistance. 

Sharing is Caring!

Leave a Reply

Your email address will not be published. Required fields are marked *