I have registered the devices in IoT and the client application (device) can update reported twin properties. Now, I have to update desired twin properties from back end application (in C#). Need help.
Asked
Active
Viewed 4,775 times
3 Answers
4
Another way of doing this is by updating the desired TwinCollection directly.
using (var manager = RegistryManager.CreateFromConnectionString("Your IoT Hub ConnectionString"))
{
var twin = await manager.GetTwinAsync("your device id");
twin.Properties.Desired["YourProperty"] = "some value";
await manager.UpdateTwinAsync(twin.DeviceId, twin, twin.ETag);
}

William Xifaras
- 5,212
- 2
- 19
- 21
3
Here's a sample on GitHub. And here's a tutorial.
Here is the relevant piece of code:
public async Task UpdateDesiredProperties(string deviceId)
{
var twin = await _registryManager.GetTwinAsync(deviceId);
var patch =
@"{
properties: {
desired: {
customKey: 'customValue'
}
}
}";
await _registryManager.UpdateTwinAsync(twin.DeviceId, patch, twin.ETag);
}

ken2k
- 48,145
- 10
- 116
- 176

Yi Zhong - MSFT
- 306
- 2
- 7
-
1Both of these samples do not change desired twin property. – nicedev80 Jan 16 '19 at 10:27
-
2This one does: https://github.com/Azure-Samples/azure-iot-samples-csharp/blob/master/iot-hub/Samples/service/RegistryManagerSample/RegistryManagerSample.cs#L135 – Yi Zhong - MSFT Jan 17 '19 at 23:16
2
Just found the way to update desired tags.
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var twin = await registryManager.GetTwinAsync(device.Id);
var patch = "{ \"properties\": { \"desired\": { \"configVersion\" : 3.1 } } }"; //json string
await registryManager.UpdateTwinAsync(device.Id, tags, twin.ETag);

nicedev80
- 1,575
- 1
- 13
- 17