Retrieve the Status Codes for Account

banner art

The following code example demonstrates how to retrieve the set of possible status codes for the account entity.

[C#]
// Set up Microsoft CRM Web service.
MetadataService service = new MetadataService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Url = "http://localhost/mscrmservices/2006/MetadataService.asmx";

try 
{
   // Get the attribute metadata for the state attribute.
   AttributeMetadata am = service.RetrieveAttributeMetadata(EntityName.account.ToString(),
                    "statecode");
   if (am.Type == AttributeType.State)
   {
      StateAttributeMetadata sm = (StateAttributeMetadata)am;
      Console.WriteLine("State");
      foreach(StateOption so in sm.Options)
      {
         Console.WriteLine(String.Concat("Description: ",
                           so.Description, " OptionValue: ",
                           so.OptionValue.ToString(), " DefaultStatus: ",
                           so.DefaultStatus.ToString()));
      }
   }

   // Get the attribute metadata for the status attribute.
   am = service.RetrieveAttributeMetadata(EntityName.opportunity.ToString(),
                           "statuscode");
   if (am.Type == AttributeType.Status)
   {
      StatusAttributeMetadata sm = (StatusAttributeMetadata)am;
      Console.WriteLine("Status");
      foreach(StatusOption so in sm.Options)
      {
         Console.WriteLine(String.Concat("Description:", so.Description, "
                           OptionValue: ",
                           so.OptionValue.ToString()));
      }
   }

   success = true;
}
catch(System.Web.Services.Protocols.SoapException ex)
{
   Console.WriteLine(String.Format("{0}. {1}", ex.Message,
                     ex.Detail.InnerText));
}
[Visual Basic .NET]
' Set up Microsoft CRM Web service.
Dim service As MetadataService = New MetadataService()
service.Credentials = System.Net.CredentialCache.DefaultCredentials
service.Url = "http://localhost/mscrmservices/2006/MetadataService.asmx"

Try
    ' Get the attribute metadata for the state attribute.
    Dim am As AttributeMetadata = service.RetrieveAttributeMetadata(EntityName.account.ToString(), _
        "statecode")
    If am.Type = AttributeType.State Then
        Dim sm As StateAttributeMetadata = CType(am, StateAttributeMetadata)
        Console.WriteLine("State")
        Dim so As StateOption
        For Each so In sm.Options
Console.WriteLine(String.Concat("Description: ", so.Description, " OptionValue: ", _
so.OptionValue.ToString(), " DefaultStatus: ", so.DefaultStatus.ToString()))
        Next
    End If

    ' Get the attribute metadata for the status attribute.
    am = service.RetrieveAttributeMetadata(EntityName.opportunity.ToString(), _
        "statuscode")
    If am.Type = AttributeType.Status Then
        Dim sm As StatusAttributeMetadata = CType(am, StatusAttributeMetadata)
        Console.WriteLine("Status")
        Dim so As StatusOption
        For Each so In sm.Options
Console.WriteLine(String.Concat("Description:", so.Description, " OptionValue: ", _
        so.OptionValue.ToString()))
        Next
    End If

    success = True
Catch ex As System.Web.Services.Protocols.SoapException
    Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText))
End Try

© 2007 Microsoft Corporation. All rights reserved.


Show: