Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Retrieve the Status Codes for Account

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

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

This sample code can be found in the following files in the SDK download:

Server\HowTo\CS\Query\RetrieveStatusCodes.cs
Server\HowTo\VB\Query\RetrieveStatusCodes.vb

For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

Example

[C#]
using System;

using CrmSdk;
using MetadataServiceSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
   /// <summary>
   /// This sample shows how to retrieve the status codes for the account entity. 
   /// </summary>
   public class RetrieveStatusCodes
   {
      static void Main(string[] args)
      {
         // TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
         RetrieveStatusCodes.Run("http://localhost:5555", "CRM_SDK");
      }
      
      public static bool Run(string crmServerUrl, string orgName)
      {
         bool success = false;

         // Set up the CRM Service.
         MetadataService service = CrmServiceUtility.GetMetadataService(crmServerUrl, orgName);

         try
         {
            RetrieveAttributeRequest attribReq = new RetrieveAttributeRequest();
            attribReq.EntityLogicalName = EntityName.opportunity.ToString();
            attribReq.LogicalName = "statecode";
            
            // Get the attribute metadata for the state attribute.
            RetrieveAttributeResponse amRes = (RetrieveAttributeResponse)service.Execute(attribReq);

            AttributeMetadata am = amRes.AttributeMetadata;

            StateAttributeMetadata stateAttrib = (StateAttributeMetadata)am;
            Console.WriteLine("State");
            foreach (StateOption so in stateAttrib.Options)
            {
               Console.WriteLine("Description: {0} OptionValue: {1} DefaultStatus: {2}", so.Label.UserLocLabel.Label, so.Value.ToString(), so.DefaultStatus.ToString());
            }

            attribReq = new RetrieveAttributeRequest();
            attribReq.EntityLogicalName = EntityName.opportunity.ToString();
            attribReq.LogicalName = "statuscode";
            
            // Get the attribute metadata for the state attribute.
            amRes = (RetrieveAttributeResponse)service.Execute(attribReq);

            am = amRes.AttributeMetadata;
         
            StatusAttributeMetadata statusAttrib = (StatusAttributeMetadata)am;
            Console.WriteLine("Status");
            foreach (StatusOption so in statusAttrib.Options)
            {
               Console.WriteLine("Description: {0} Option Value: {1}", so.Label.UserLocLabel.Label, so.Value.ToString());
            }
      
            success = true;
         }
         catch (System.Web.Services.Protocols.SoapException ex)
         {
            Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText));
         }

         return success;
      }
   }
}
[Visual Basic .NET]
Imports System
 
Imports CrmSdk
Imports MetadataServiceSdk
Imports Microsoft.Crm.Sdk.Utility
 
Namespace Microsoft.Crm.Sdk.HowTo

   '/ <summary>
   '/ This sample shows how to retrieve the status codes for the account entity. 
   '/ </summary>
   Public Class RetrieveStatusCodes

      Sub Main()

         ' TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
         RetrieveStatusCodes.Run("http://localhost:5555", "CRM_SDK")

      End Sub
      
      Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) As Boolean
         Dim success As Boolean =  False 
 
         ' Set up the CRM Service.
         Dim service As MetadataService =  CrmServiceUtility.GetMetadataService(crmServerUrl, orgName) 
 
         Try
            Dim attribReq As RetrieveAttributeRequest =  New RetrieveAttributeRequest() 
            attribReq.EntityLogicalName = EntityName.opportunity.ToString()
            attribReq.LogicalName = "statecode"
 
            ' Get the attribute metadata for the state attribute.
            Dim amRes As RetrieveAttributeResponse = CType(service.Execute(attribReq), RetrieveAttributeResponse)
 
            Dim am As AttributeMetadata =  amRes.AttributeMetadata 
 
            Dim stateAttrib As StateAttributeMetadata = CType(am, StateAttributeMetadata)
            Console.WriteLine("State")
            
            Dim so As StateOption
            
            For Each so In stateAttrib.Options
               Console.WriteLine("Description: {0} OptionValue: {1} DefaultStatus: {2}", so.Label.UserLocLabel.Label, so.Value.ToString(), so.DefaultStatus.ToString())
            Next
 
            attribReq = New RetrieveAttributeRequest()
            attribReq.EntityLogicalName = EntityName.opportunity.ToString()
            attribReq.LogicalName = "statuscode"
 
            ' Get the attribute metadata for the state attribute.
            amRes = CType(service.Execute(attribReq), RetrieveAttributeResponse)
 
            am = amRes.AttributeMetadata
 
            Dim statusAttrib As StatusAttributeMetadata = CType(am, StatusAttributeMetadata)
            Console.WriteLine("Status")
            
            Dim sso As StatusOption
            For Each sso In statusAttrib.Options
               Console.WriteLine("Description: {0} Option Value: {1}", sso.Label.UserLocLabel.Label, sso.Value.ToString())
            Next
 
            success = True
         Catch ex As System.Web.Services.Protocols.SoapException
            Console.WriteLine(String.Format("{0}. {1}", ex.Message, ex.Detail.InnerText))
         End Try
 
         Return success
      End Function
   End Class
End Namespace

See Also

Reference


© 2010 Microsoft Corporation. All rights reserved.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.