Im writing a class with witch creates a overview over the employees phones.
I am getting the info form Activesync object containing phones as children.
This is my current code. It works if the child dont contain any nulls.
foreach (DirectoryEntry child in directoryObject.Children)
{
var activeSyncPhone = new ActiveSync.Phone();
activeSyncPhone.Cn = child.Properties["cn"].Value.ToString(); //string
activeSyncPhone.DistinguishedName = child.Properties["distinguishedName"].Value.ToString(); //sting
activeSyncPhone.InstanceType = (int)child.Properties["instanceType"].Value; //int
activeSyncPhone.WhenCreated = (DateTime)child.Properties["whenCreated"].Value; //datetime
activeSyncPhone.WhenChanged = (DateTime)child.Properties["whenChanged"].Value; //datetime
activeSyncPhone.Name = child.Properties["name"].Value.ToString(); //string
activeSyncPhone.ObjectCategory = child.Properties["objectCategory"].Value.ToString(); //string
activeSyncPhone.MsExchFirstSyncTime = (DateTime)child.Properties["msExchFirstSyncTime"].Value;//datetime
activeSyncPhone.MsExchDeviceEASVersion = child.Properties["msExchDeviceEASVersion"].Value.ToString();//string
activeSyncPhone.MsExchDeviceFriendlyName = child.Properties["msExchDeviceFriendlyName"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceAccessState = (ActiveSync.Phone.DeviceAccessState)child.Properties["msExchDeviceAccessState"].Value; //int
activeSyncPhone.MsExchDeviceID = child.Properties["msExchDeviceID"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceType = child.Properties["msExchDeviceType"].Value.ToString(); //string
try
{
activeSyncPhone.MsExchDeviceIMEI = child.Properties["msExchDeviceIMEI"]?.Value.ToString(); //string
}
catch
{
activeSyncPhone.MsExchDeviceIMEI = "Could not find IMEI";
}
activeSyncPhone.MsExchDeviceUserAgent = child.Properties["msExchDeviceUserAgent"].Value.ToString(); //string
activeSyncPhone.MsExchVersion = child.Properties["msExchVersion"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceAccessStateReason = (ActiveSync.Phone.DeviceAccessStateReason)child.Properties["msExchDeviceAccessStateReason"].Value; //string
activeSyncPhone.MsExchUserDisplayName = child.Properties["msExchUserDisplayName"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceModel = child.Properties["msExchDeviceModel"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceOS = child.Properties["msExchDeviceOS"].Value.ToString(); //string
activeSyncPhone.ObjectGUID = child.Properties["objectGUID"].Value.ToString(); //string
activeSyncUnits.PhoneList.Add(activeSyncPhone);
child.Close();
}
directoryObject.Close();
I was wondering if there was any way to make this a bit more robust. I was looking into setting the ActiveSyncPhone's propertys dynamically and then with a list set all the properties. But C# is a strongly-typed language and I figured id take advantage of the type safety and performance advantages that accompany that aspect.
I think there might be a better way then checking every child.property for null with if statements? and aslo is there a better way for getting the child properties?
You could create a generic function for that:
// you'll have to figure out the type of the `child.Properties`
public static T GetValue<T>(TypeOfChildProperties properties, string name, T defaultValue = default(T))
{
var value = properties[name];
if (value == null)
return defaultValue;
return (T)value;
// if you have some cast problems, you could use this:
return (T)Convert.ChangeType(value, typeof(T));
}
activeSyncPhone.Cn = GetValue<string>(child.Properties, "cn");
activeSyncPhone.DistinguishedName = GetValue<string>(child.Properties, "distinguishedName");
activeSyncPhone.InstanceType = GetValue<int>(child.Properties, "instanceType");
activeSyncPhone.MsExchDeviceIMEI = GetValue<string>(child.Properties, "msExchDeviceIMEI", "Could not find IMEI");