InsertOptionValue Message (MetadataService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Works for all deployment types Works online only

Inserts an option value into a list of options.

The relevant classes are specified in the following table.

Type Class
Request InsertOptionValueRequest
Response InsertOptionValueResponse

Remarks

To perform this action, the caller must be a user in the organization for which metadata is requested and must have Write Attribute privileges.

You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.

Example

The following example shows how to use the InsertOptionValue message.

[C#]
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";

// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0; 

// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;

// Create a new picklist label.
MetadataServiceSdk.CrmLabel newCrmLabel = new MetadataServiceSdk.CrmLabel();
MetadataServiceSdk.CrmNumber langCode = new MetadataServiceSdk.CrmNumber();
MetadataServiceSdk.LocLabel englishLabel = new MetadataServiceSdk.LocLabel();

// Use US English language code.
langCode.Value = 1033;
englishLabel.LanguageCode = langCode;
englishLabel.Label = "New Picklist Label";
newCrmLabel.LocLabels = new MetadataServiceSdk.LocLabel[] { englishLabel };

// Create a request.
InsertOptionValueRequest insertRequest = new InsertOptionValueRequest();

// Insert new label text into an existing picklist.
insertRequest.AttributeLogicalName = "address1_addresstypecode";
insertRequest.EntityLogicalName = EntityName.contact.ToString();
insertRequest.Label = newCrmLabel;
insertRequest.Value = new MetadataServiceSdk.CrmNumber();

// Microsoft Dynamics CRM 4.0 picklist values start at 200,000 to maintain backwards compatibility with Microsoft Dynamics CRM 3.0.
insertRequest.Value.Value = 200000;

// Send the message.
InsertOptionValueResponse insertResponse = (InsertOptionValueResponse)metadataService.Execute(insertRequest);

// Publish the new custom entity.
// Note: All customizations must be published before they can be used.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);

[Visual Basic .NET]
' Set up the CRM service.
Dim token As New CrmAuthenticationToken()
' You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";

// Create the metadata Web service.
Dim metadataService As New MetadataService ()
metadataService.Url = "http://<servername>:<port>/mscrmservices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials
metadataService.PreAuthenticate = true;

' Create a new picklist label.
Dim newCrmLabel As New MetadataServiceSdk.CrmLabel()
Dim langCode As New MetadataServiceSdk.CrmNumber()
Dim englishLabel As New MetadataServiceSdk.LocLabel()

' Use the US English language code.
langCode.Value = 1033
englishLabel.LanguageCode = langCode
englishLabel.Label = "New Picklist Label"
newCrmLabel.LocLabels = New MetadataServiceSdk.LocLabel() { englishLabel }

' Create a request.
Dim insertRequest As New InsertOptionValueRequest()

' Insert new label text into an existing picklist.
insertRequest.AttributeLogicalName = "address1_addresstypecode"
insertRequest.EntityLogicalName = EntityName.contact.ToString()
insertRequest.Label = newCrmLabel
insertRequest.Value = New MetadataServiceSdk.CrmNumber()

' Microsoft CRM 4.0 picklist values start at 200,000 to maintain backwards compatibility with 3.0.
insertRequest.Value.Value = 200000

' Send the message.
Dim insertResponse As InsertOptionValueResponse = CType(metadataService.Execute(insertRequest), InsertOptionValueResponse)

' Publish the new custom entity.
' Note:  All customizations must be published before they can be used.
Dim publishAllRequest As New PublishAllXmlRequest()
Dim publishAllResponse As PublishAllXmlResponse = CType(metadataService.Execute(publishAllRequest), PublishAllXmlResponse)

See Also

Concepts

Reference

© 2010 Microsoft Corporation. All rights reserved.