How to Get the Display Name of an Enumeration Value Programmatically

Awhile back I wrote a blog post about how to programmatically manipulate Enumerations (aka List Values.  See blog post – Programmatically Working with Enumerations.

One thing I didn’t really explain was how to get the DisplayString of an Enumeration value when you are coming at it from an object.  For example, let’s say you have an incident object and you want to display the display name of the current value of the Urgency impact property (which is an enum data type property).

What you need to keep in mind here is that you can’t treat it like other string, int, etc. data type properties.  Non-enum properties can be accessed like this:

emopIncident.Object[classIncident, “Title”].Value

If you want to you can explicitly cast the value to a string by slapping a .ToString() on the end.

emopIncident.Object[classIncident, “Title”].Value.ToString()

If you do that for an enum data type property you are going to get this kind of thing back: System.WorkItem.TroubleTicket.UrgencyEnum.High.  That’s the ID of the enum not the display name!

To get the display name of the enum you need to keep in mind that the value that is going to be coming back for an enum data type property is a ManagementPackEnumeration.  You just need to cast the .Value to ManagementPackEnumeration and then you can access the .DisplayName property.

Sample code – just a simple console app to demonstrate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace GetEnumerationValueDisplayString
    class Program
        static void Main(string[] args)
            EnterpriseManagementGroup emg = new EnterpriseManagementGroup("localhost");
            ManagementPackClass classIncident = emg.EntityTypes.GetClass(new Guid("A604B942-4C7B-2FB2-28DC-61DC6F465C68")); //System.WorkItem.Incident
            ManagementPackTypeProjection mptpIncident = emg.EntityTypes.GetTypeProjection(new Guid("285CB0A2-F276-BCCB-563E-BB721DF7CDEC")); //System.WorkItem.Incident.ProjectionType
            ObjectProjectionCriteria opcAllIncidents = new ObjectProjectionCriteria(mptpIncident);
            IObjectProjectionReader<EnterpriseManagementObject> oprIncidents = emg.EntityObjects.GetObjectProjectionReader<EnterpriseManagementObject>(opcAllIncidents, ObjectQueryOptions.Default);
            foreach (EnterpriseManagementObjectProjection emopIncident in oprIncidents)
                //This is how you handle string, int, etc. properties:
                Console.WriteLine(emopIncident.Object[classIncident, "Title"].Value.ToString());

                //DON'T do this for enum data type properties

                //Do this instead:
                ManagementPackEnumeration enumUrgency = (ManagementPackEnumeration)emopIncident.Object[classIncident, "Urgency"].Value;

Sample VS project attached.