VMware Cloud Community
gislig
Contributor
Contributor
Jump to solution

I´m trying to read CustomField Attributes using VMWare.VIM from C#

Hi, I´m trying to read customField attributes on folders from C#, but the problem is that it only contains these functions which I can use but it does not contain the Value

Here is the code

var vmFolderList = vmClient.FindEntityViews(typeof(Folder), null, null, null);

foreach (Folder vmFolder in vmFolderList)

{

     foreach (CustomFieldDef thisField in vmFolder.AvailableField)

          if (thisField.Name.Equals("SomeThing")){

               // Here are the available objects

               thisField.FieldDefPrivileges

               thisField.FieldInstancePrivileges

               thisField.Key

               thisField.ManagedObjectType

               thisField.Name

               thisField.Type

          }

     }

}

Missing thisField .Value, it seems to be available in PowerShell and I don´t seem to be able to figure out where the value for the key is. If anyone knows how to get the value from the key please let me know ?

Reply
0 Kudos
1 Solution

Accepted Solutions
gislig
Contributor
Contributor
Jump to solution

Here is the solution if anyone is doing the same thing

            // Get all the folders in vmware

            var vmFolderList = vmClient.FindEntityViews(typeof(Folder), null, null, null);

            // Itterate through the folders

            foreach (Folder v in vmFolderList)

            {

                // Create variables to assign CustomFields

                int key = 0;

                string EValue = string.Empty;

                // Search for the name of the custom field

                foreach (CustomFieldDef tf in v.AvailableField)

                {

                    if (tf.Name.Equals("CustomFieldToSearchFor"))

                    {

                        // Set the key for the custom field

                        key = tf.Key;

                    }

                }

                // Connect with the ExtensibleManagedObject with the client connection and the moref identification

                ExtensibleManagedObject extensible = new ExtensibleManagedObject(vmClient, v.MoRef);

                // Itterate through the CustomFiledStringValues and get the values

                foreach (CustomFieldStringValue ta in v.Value)

                {

                    // Check if the key variable contains key number and if so

                    if (key.ToString().Equals(ta.Key.ToString())) {

                        // Assign the value to EValue

                        EValue = ta.Value;

                    }

                }

                // Output the data

                Console.WriteLine("[" + EValue + "] - [" + v.MoRef + "] - [" + v.Name + "]");

            }

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

CustomFields are spread over 2 properties on the ExtensibleManagedObject object, the AvailableField and the Value properties.

This PowerShell prototype shows how to get the value of a field.

$folderName = 'MyFolder'

$folder = Get-Folder -Name $folderName

$customFieldName = 'Field1'

$field = $folder.ExtensionData.AvailableField | where{$_.Name -eq $customFieldName}

$folder.ExtensionData.Value | where{$_.Key -eq $field.Key} | select -ExpandProperty Value


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
gislig
Contributor
Contributor
Jump to solution

Thanks but I need some help to get the ExpandProperty from the C# code, I´m not sure how I can implement it i.e. the ExtensibleManagementObject, if you would have any demo or sample code on how to get the data in C# it would be great.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No C# examples I'm afraid.

But the ExpandProperty is an option for the Select-Object cmdlet.

Instead of returning the requested properties in another object, the ExpandProperty returns the scalar (just the value).

So I guess in C# you should just read the Value field.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
gislig
Contributor
Contributor
Jump to solution

Here is the solution if anyone is doing the same thing

            // Get all the folders in vmware

            var vmFolderList = vmClient.FindEntityViews(typeof(Folder), null, null, null);

            // Itterate through the folders

            foreach (Folder v in vmFolderList)

            {

                // Create variables to assign CustomFields

                int key = 0;

                string EValue = string.Empty;

                // Search for the name of the custom field

                foreach (CustomFieldDef tf in v.AvailableField)

                {

                    if (tf.Name.Equals("CustomFieldToSearchFor"))

                    {

                        // Set the key for the custom field

                        key = tf.Key;

                    }

                }

                // Connect with the ExtensibleManagedObject with the client connection and the moref identification

                ExtensibleManagedObject extensible = new ExtensibleManagedObject(vmClient, v.MoRef);

                // Itterate through the CustomFiledStringValues and get the values

                foreach (CustomFieldStringValue ta in v.Value)

                {

                    // Check if the key variable contains key number and if so

                    if (key.ToString().Equals(ta.Key.ToString())) {

                        // Assign the value to EValue

                        EValue = ta.Value;

                    }

                }

                // Output the data

                Console.WriteLine("[" + EValue + "] - [" + v.MoRef + "] - [" + v.Name + "]");

            }

Reply
0 Kudos