Code to retrieve status value based on status label in CRM 3

This code is for retrieving status value based on status text for salesorder entity.


public int RetrieveStatusValue(EntityName Entity, string StatusValue)

       int intStatus = 0;

       MetadataService MetaService = new MetadataService();
       MetaService.Credentials = System.Net.CredentialCache.DefaultCredentials;

       AttributeMetadata am = MetaService.RetrieveAttributeMetadata(Entity.ToString(), "statuscode");
       StatusAttributeMetadata sm = (StatusAttributeMetadata)am;

       foreach (StatusOption so in sm.Options)
           if (so.State == 0)
               if (so.Description.Trim().ToLower() == StatusValue.ToLower())
                   intStatus = so.OptionValue;

       am = null;
       sm = null;


       return intStatus;


To call the function

RetrieveStatusValue(EntityName.salesorder, "Draft");




Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

Please share your thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.