Retrieve the Localized State Name
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
Find the latest SDK documentation: CRM 2015 SDK
This sample shows how to retrieve the localized state name for an attribute.
This sample code can be found in the following files in the SDK download:
Server\HowTo\CS\Query\RetrieveLocalizedState.cs
Server\HowTo\VB\Query\RetrieveLocalizedState.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 localized state name for an attribute.
/// </summary>
public class RetrieveLocalizedState
{
static void Main(string[] args)
{
// TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
RetrieveLocalizedState.Run("https://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 sm = (StateAttributeMetadata)am;
foreach (StateOption so in sm.Options)
{
Console.WriteLine(String.Concat("Description:", so.Label.UserLocLabel.Label, " OptionValue: ",
so.Value.ToString(), " DefaultStatus: ", so.DefaultStatus.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 localized state name for an attribute.
'/ </summary>
Public Class RetrieveLocalizedState
Sub Main()
' TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
RetrieveLocalizedState.Run("https://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 sm As StateAttributeMetadata = CType(am, StateAttributeMetadata)
Dim so As StateOption
For Each so In sm.Options
Console.WriteLine(String.Concat("Description: {0} OptionValue: {1} DefaultStatus: {2}", so.Label.UserLocLabel.Label, so.Value.ToString(), so.DefaultStatus.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
.gif)