Retrieve the Localized State Name
This sample shows how to retrieve the localized state name for an attribute.
Example
[C#]
using System;
using MetadataServiceSdk;
using CrmSdk;
namespace Microsoft.Crm.Sdk.HowTo
{
/// <summary>
/// This sample shows how to retrieve the localized state name for an
/// attribute.
/// </summary>
class RetrieveLocalizedState
{
[STAThread]
public static void Main(string[] args)
{
Run();
}
public static bool Run()
{
bool success = false;
// Set up Microsoft CRM Web service.
MetadataService service = new MetadataService();
service.Credentials =
System.Net.CredentialCache.DefaultCredentials;
//service.Url =
// "https://localhost/mscrmservices/2006/MetadataService.asmx";
try
{
// Get the attribute metadata for the state attribute.
AttributeMetadata am =
service.RetrieveAttributeMetadata(
EntityName.opportunity.ToString(),
"statecode");
if (am.Type == AttributeType.State)
{
StateAttributeMetadata sm = (StateAttributeMetadata)am;
foreach(StateOption so in sm.Options)
{
Console.WriteLine(String.Concat("Description:",
so.Description, " OptionValue: ",
so.OptionValue.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 MetadataServiceSdk
Imports CrmSdk
Namespace Microsoft.Crm.Sdk.HowTo
'/ <summary>
'/ This sample shows how to retrieve the localized state name for an
'/ attribute.
'/ </summary>
Class RetrieveLocalizedState
<STAThread()> _
Public Shared Sub Main(ByVal args() As String)
Run()
End Sub
Public Shared Function Run() As Boolean
Dim success As Boolean = False
' Set up Microsoft CRM Web service.
Dim service As MetadataService = New MetadataService
service.Credentials = _
System.Net.CredentialCache.DefaultCredentials
'service.Url =
' "https://localhost/mscrmservices/2006/MetadataService.asmx"
Try
' Get the attribute metadata for the state attribute.
Dim am As AttributeMetadata = _
service.RetrieveAttributeMetadata( _
EntityName.opportunity.ToString(), _
"statecode")
If am.Type = AttributeType.State Then
Dim sm As StateAttributeMetadata = _
CType(am, StateAttributeMetadata)
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
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