cancel
Showing results for 
Search instead for 
Did you mean: 

Afaria 7SP5: Sample code for GetDeviceProperties

Former Member
0 Kudos

Hi,

I'm looking to use the API to get the MDM version of Samsung Devices

I'm aware of which has been useful, but I get lost with the lack of context (I'm not a developer)

So, my request is this:

I'm looking for some sample code to, for a given array of devices:

String[] idsArray = new string[] { "{XXXXXX-XXX-XXX-XXXX-XXXXXXXXXX}" };

to retrieve, using Class Security and Property MDM Version the information

If you wish to throw a different class and property in there, so I can see how that would be done, it would aid my learning

Kind regards,

Paul

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Paul,

I have provided sample code below that shows how to use the DevicePropertiesRequest and GetDeviceProperties calls to retrieve the data you require.

static void Main(string[] args)
        {
            DevicesServiceClient deviceClient = new DevicesServiceClient("NetTcpBinding_IDevicesService", "net.tcp://<API Server IP Address>/AfariaService/Devices");
            System.Net.NetworkCredential APICredentials = new System.Net.NetworkCredential("User", "Password", "Domain.com");
            deviceClient.ClientCredentials.Windows.ClientCredential = APICredentials;

            deviceClient.InitContext(Guid.NewGuid().ToString());
            deviceClient.SetTenantIdContext(0);

            DataState dataState = new DataState();
            String[] deviceIds = deviceClient.GetFirstIds(0, -1, SupportedClients.Android, SearchFields.None, "", out dataState);
            DeviceInfo[] deviceInfo = deviceClient.GetDeviceInfo(DeviceInfoDataRetrievalTypes.DeviceIdInfo, deviceIds);

            DevicePropertiesRequest request = new DevicePropertiesRequest();
            request.ReturnSchemaClassPropertyDefinitions = false;

            request.DBIds = new DevicePropertiesId[deviceIds.Length];

            int i = 0;
            foreach (String id in deviceIds)
            {
                request.DBIds[i] = new DevicePropertiesId();
                request.DBIds[i].ClientType = ClientType.Android;
                request.DBIds[i].DBId = int.Parse(deviceInfo[i].ClientUid);
                i++;
            }


            request.RequestParameters = new DevicePropertiesRequestParameter[1];
            request.RequestParameters[0] = new DevicePropertiesRequestParameter();
            request.RequestParameters[0].ClientType = ClientType.Android;
            request.RequestParameters[0].ClassPropertiesList = new DevicePropertiesRequestClassProperty[1];
            request.RequestParameters[0].ClassPropertiesList[0] = new DevicePropertiesRequestClassProperty();
            request.RequestParameters[0].ClassPropertiesList[0].ClassName = "Security";
            request.RequestParameters[0].ClassPropertiesList[0].PropertyNames = new String[1];
            request.RequestParameters[0].ClassPropertiesList[0].PropertyNames[0] = "MDM Version";

            DataSetSurrogate dataSetSurrogate = deviceClient.GetDeviceProperties(request);
            System.Data.DataSet dataSet = DataSetSurrogateEx.ConvertToDataSet(dataSetSurrogate);

            foreach (System.Data.DataTable dataTable in dataSet.Tables)
            {
                Console.WriteLine("Table: " + dataTable.ToString());
                Console.WriteLine("*********************");

                foreach (System.Data.DataColumn column in dataTable.Columns)
                {
                    Console.Write(column.ToString() + "\t");
                }
                Console.WriteLine("\n");

                foreach (System.Data.DataRow row in dataTable.Rows)
                {
                    foreach (Object obj in row.ItemArray)
                    {
                       
                        if (obj != null && !string.IsNullOrWhiteSpace(obj.ToString()))
                        {
                            Console.Write(obj.ToString() + "\t");
                        }
                        else
                        {
                            Console.Write("null\t");
                        }
                    }
                    Console.WriteLine("\n\n");

                }
            }

            Console.WriteLine("*********************");
            Console.Write("Press any key to continue...");
            Console.ReadLine();

           

        }

Regards,

Taj Martin

SAP Active Global Support

Former Member
0 Kudos

Getting so close!

Have had to adapt the code for a couple of reasons:

Was getting: Cannot implicitly convert type 'AllServiceTypes.Alerts.ClientType' to 'AllServiceTypes.Devices.ClientType'. An explicit conversion exists (are you missing a cast?)

So, as you'll see, I do a GetDeviceInfo first to get the devices, then use a loop to cycle through these to get the DBIds and also the client types

        static void Main(string[] args)

        {

            string apiAddress = "somename"; // Dev

            string apiUserDomain = "somedomain";

            string apiUserName = "someuser";

            string apiUserPass = "somepass";

            DevicesServiceClient DevicesClient = new DevicesServiceClient("NetTcpBinding_IDevicesService", "net.tcp://" + apiAddress + ":7982/AfariaService/Devices");

            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(apiUserName, apiUserPass, apiUserDomain);

            DevicesClient.ClientCredentials.Windows.ClientCredential = credentials;

            DevicesClient.InitContext(Guid.NewGuid().ToString());

            DevicesClient.SetTenantIdContext(0);

            String[] deviceIds = new string[] { "129", "130" };

            DeviceInfo[] deviceInfo = DevicesClient.GetDeviceInfo(DeviceInfoDataRetrievalTypes.DeviceIdInfo, deviceIds);

            DevicePropertiesRequest request = new DevicePropertiesRequest();

            request.ReturnSchemaClassPropertyDefinitions = false;

            request.DBIds = new DevicePropertiesId[deviceIds.Length];

            int i = 0;

            foreach (Devices.DeviceInfo info in deviceInfo)

            {

                request.DBIds[i] = new DevicePropertiesId();

                request.DBIds[i].ClientType = info.ClientType;

                request.DBIds[i].DBId = int.Parse(deviceInfo[i].ClientUid);

                i++;

            }

            request.RequestParameters = new DevicePropertiesRequestParameter[1];

            request.RequestParameters[0] = new DevicePropertiesRequestParameter();

            request.RequestParameters[0].ClientType = request.DBIds[0].ClientType;

            request.RequestParameters[0].ClassPropertiesList = new DevicePropertiesRequestClassProperty[1];

            request.RequestParameters[0].ClassPropertiesList[0] = new DevicePropertiesRequestClassProperty();

            request.RequestParameters[0].ClassPropertiesList[0].ClassName = "Security";

            request.RequestParameters[0].ClassPropertiesList[0].PropertyNames = new String[1];

            request.RequestParameters[0].ClassPropertiesList[0].PropertyNames[0] = "MDM Version";

            Devices.DataSetSurrogate dataSetSurrogate = DevicesClient.GetDeviceProperties(request);

          

            Console.WriteLine("*********************");

            Console.Write("Press any key to continue...");

            Console.ReadLine();

        // end

        }

This works fine (which is an achievement, to say the least!). The major difference between my first attempt was that I was creating a new request for each device, rather the above which is an array of devices, but one set of parameters.

Now, having issues with the DataSet… See my next post...

Former Member
0 Kudos

So, DataSet:

            Devices.DataSetSurrogate dataSetSurrogate = DevicesClient.GetDeviceProperties(request);

          

            System.Data.DataSet dataSet = Devices.DataSetSurrogate.ConvertToDataSet(dataSetSurrogate);

            foreach (System.Data.DataTable dataTable in dataSet.Tables)

            {

                Console.WriteLine("Table: " + dataTable.ToString());

                Console.WriteLine("*********************");

                foreach (System.Data.DataColumn column in dataTable.Columns)

                {

                    Console.Write(column.ToString() + "\t");

                }

                Console.WriteLine("\n");

                foreach (System.Data.DataRow row in dataTable.Rows)

                {

                    foreach (Object obj in row.ItemArray)

                    {

                        if (obj != null && !string.IsNullOrWhiteSpace(obj.ToString()))

                        {

                            Console.Write(obj.ToString() + "\t");

                        }

                        else

                        {

                            Console.Write("null\t");

                        }

                    }

                    Console.WriteLine("\n\n");

                }

            }

'AllServiceTypes.Devices.DataSetSurrogate' does not contain a definition for 'ConvertToDataSet'

AllServiceTypes is my project name / namespace

Have tried Devices, DevicesClient (the name of my DevicesServiceClient ), without (but then it gets confused with other service types and mentions ambiguity)


Ideas?


Former Member
0 Kudos

I see that this is a class created by SAP, which is why it's having issues

Any chance of a copy?

Answers (1)

Answers (1)

ercin_nurol
Explorer
0 Kudos

Hi Paul,

can you please upload a screenshot of the Afaria Administrator that shows what information you exactly want to gather with the API?

For instance the MDM Version of devices is collected via the GetDeviceInfo method of the IDevices Service.

An example would be:

AfariaServiceDevices.DevicesServiceClient deviceClient = new AfariaServiceDevices.DevicesServiceClient("NetNamedPipeBinding_IDevicesService");

string[] android = deviceClient.GetFirstIds(0,1000,AfariaServiceDevices.SupportedClients.Android, AfariaServiceDevices.SearchFields.All , "", out state)

List<String> android_myList = android.ToList();

String[] android_ids = android_myList.ToArray()

AfariaServiceDevices.DeviceInfo[] deviceInfoTbl;

deviceInfoTbl = deviceClient.GetDeviceInfo(AfariaServiceDevices.DeviceInfoDataRetrievalTypes.DeviceIdINfo, android_ids);

foreach (AfariaServiceDevices.DeviceInfo mydevices in deviceInfoTbl)

{

Console.WriteLine("Android-Version " + mydevices.PlatformVersion);

Console.WriteLine("Under MDM Control?  " + mydevices.Managed);

Console.WriteLine("ClientGuid  " + mydevices.Managed);

}

Regards

Ercin Nurol

SAP Active Global Support