VMware Cloud Community
Ondit
Contributor
Contributor

VMWare VIM SDK .Net, update CustomFieldValue

Hi,

I am not sure if this is the correct forum for SDK questions, please redirect me to a correct one if not, but here goes:

I have this code:

public void SetFieldValue(int key, string value)

{

    var customField = (CustomFieldStringValue)this.VirtualMachine.CustomValue.Single(cv => cv.Key == key);

    customField.Value = value;

}

That in itself works, as in, it's visible that the value has changed, but I don't see it reflected upon the actual custom field on the VM when viewed in the vSphere Client.

So, is there a save I need to do, or do I have to change the value in another fashion?

Thanks

Message was edited by: Ondit

I've gotten a bit further, looks like I need to use the CustomFieldsManager, but I don't know what to provide as Entity for the SetField method:

var mgr = new CustomFieldsManager(this.VimClient, myVM.VirtualMachine.MoRef);

mgr.SetField(???, key, value);

0 Kudos
2 Replies
jeffpatton
Enthusiast
Enthusiast

old thread I know, hoping someone else lately has come across this. Ondit, I've gotten a bit further, but am at more or less the same place

ManagedEntity vmEntity = new ManagedEntity(vimClient, clonedVM.MoRef);            

CustomFieldsManager fieldManager = new CustomFieldsManager(vimClient, clonedVM.MoRef);           

//           

// One or more custom field names could be stored in the web.config and processed in some fashion           

//           

foreach (CustomFieldDef thisField in clonedVM.AvailableField)           

{               

if (thisField.Name.Equals("CreatedBy"))               

{                   

fieldManager.SetField(vmEntity.MoRef, thisField.Key, txtUsername.Text);               

}           

}

I saw some perl code that appeared to cast a vm as an entity, but doing a direct cast (ManagedEntity)clonedVM appears to make the method unhappy.

Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 http://patton-tech.com
0 Kudos
jeffpatton
Enthusiast
Enthusiast

Ondit

I have a working version now, hopefully you can figure out the perl version from my .net version.

CustomFieldsManager fieldManager = client.GetView(client.ServiceContent.CustomFieldsManager, null);

foreach (CustomFieldDef thisField in fieldManager.Field)
{

if (thisField.Name.Equals("CreatedBy"))

{

fieldManager.SetField(clonedVM.MoRef, thisField.Key, sUsername);

}

}

I start by instantiating a customfieldsmanager object (fieldManager) using the GetView method of the vimclient (client). GetView needs a moRef to get the thing you're after, so I pull in the client's ServiceContent.CustomFieldsManager which is a moRef. I don't recall where I saw this, but that was the piece that was eluding me. fieldManager has a SetField method, but when I was originally connecting to it, I was CREATING IT NEW, so it was always null. Once fieldManager was managing the customfields from vsphere I was able to iterate over them in the foreach loop. Then down inside I test to see if the field i'm currently on matches a field I know exists, and then I set it. In my case, CreatedBy is a string field, and sUsername is a string containing the username.

Hope this helps, this has really been bugging me for a while, I've wanted to include this functionality for a while now, but I've not been able to get it working properly.

Good Luck!

Jeffrey S. Patton Systems Specialist, Enterprise Systems University of Kansas 1001 Sunnyside Ave. Lawrence, KS. 66045 http://patton-tech.com
0 Kudos