Tagging: What’s the Alternative?

After my last post Tagging: An invisible feature in vSphere, William Lam quickly followed up with another post in his blog, arguing vSphere Tagging Feature Not So Invisible. He made his case by showing how you can actually add and remove tags using addTag() and removeTag() method via vSphere MOB. You can definitely play with your test environment, but please do not mess up with any existing tags in your production environment.

Although you can do that via MOB, it’s not the officially supported way (BTW, this should not be taken as show stopper. After all, the Client REST API in VI Java API 2.0 is based on MOB). It subjects to change in the future. The ideal way is via vSphere APIs. As I mentioned, there is no official APIs for that. The two methods William mentioned are not formally exposed.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

Now, what if you want to achieve the same result with official vSphere APIs? Or, is it achievable at all?

The answer is yes. As I covered in my book, there is a managed object type called CustomFieldsManager. What it does is to add and remove custom fields to managed entities. The custom fields are essentially key/value pairs that are attached to managed entities. They are not properties per se but can be accessed via the customValue property (type: CustomFieldValue[]) of a ManagedEntity object. The CustomFieldValue is a key/value pair. As of vSphere 4.1, the value is only string type.

With these basic understanding, you can start to implement tagging with CustomFieldsManager. First, you want to call addCustomFieldDef() method to create a custom field whose name is tag. Then you can call setField() method to set the value of the tag. Remember, you can have only one string for the tag. For more keywords, you can just use “,” or “;” as delimiter, for example, “development;pilot 101.” Unlike the tag property, which is array of Tag object, you have to parse them out while reading and compile them while writing. But it’s pretty trivial task for a programmer.

When you add tag with CustomFieldsManager, it’s really something for yourself unless tagging is explicitly displayed or made searchable via vSphere Client GUI. So far there is no standard tag keyword that we all agree upon.

This entry was posted in vSphere API and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted June 13, 2011 at 12:23 am | Permalink


    afaik, CustomFieldsManager only allows you to set a custom field for a Host or VM, it’s not available to all entities such as datacenter, datastore, network, etc. which the actual “tagging” feature supports. Though not a complete replacement/substitute, if you needed to tag either a VM and/or Host, then it’s definitely an option. Hopefully VMware will expose these methods in the future as they can be very useful.


  2. Posted June 13, 2011 at 1:07 am | Permalink

    According to API reference (see the AddCustomFieldDef() method of CustomFieldsManager):

    Creates a new custom field. If the moType is specified, the field will only be available for that type of managed object.

    It means by default, a field definition will be available to all entities including datacenter, folder. Only with and after 2.5 can you specify what exact type of entity to define a new field.
    I agree with you that these methods can be very useful.

  3. Posted June 13, 2011 at 9:44 pm | Permalink

    @Steve Jin

    I noticed that earlier while looking at the API reference but I thought from the way it was exposed via the vSphere Client that it was limited to just VM & Hosts. I just gave it a try and it in fact you’re right, any of the managed objects will work but just won’t be visible via the client which is too bad. Definitely another option if someone is looking to tag custom attributes without resorting to a separate DB.

    Great post


  4. Posted June 14, 2011 at 1:00 am | Permalink


    Thanks a lot for taking time to validate it and sharing with us the result!


One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.