Saturday, July 18, 2009

CRM 4.0 Setting a Picklist Default Value


Currently Dynamics supports setting default values for two attribute types: PicklistAttributeMetaddata (Picklist) and BooleanAttributeMetadata (bit fields). If you intend to set the default using custom code you need to follow the below process:

1. Retrieve the attribute metadata (unless you’re creating a new one) using RetrieveAttributeRequest
2. Cast the returned AttributeMetadata to a PicklistAttributeMetadata.
3. Set the picklist metadata Default value to an integer of you choice.
4. Update the attribute metadata using UpdateAttribute Request.
5. Publish the changes using PublishXml Request.

The bellow code is a working example which sets the account Relationship Type (customertypecode) picklist to Customer (value = 3).


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.SdkTypeProxy.Metadata;

namespace GI.Sendbox
{
class Program
{
static void Main(string[] args)
{
try
{
/* Create Authenticatio Token */
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "MicrosoftCRM";

/* Create a CrmService end point */
CrmService crmService = new CrmService();
crmService.UnsafeAuthenticatedConnectionSharing = true;
crmService.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
crmService.UseDefaultCredentials = true;
crmService.CrmAuthenticationTokenValue = token;

/* Create a MetadataService end point */
MetadataService metaService = new MetadataService();
metaService.Url = "http://localhost:5555/mscrmservices/2007/metadataservice.asmx";
metaService.UseDefaultCredentials = true;
metaService.UnsafeAuthenticatedConnectionSharing = true;
metaService.CrmAuthenticationTokenValue = token;

/* Retrieve the attribute metadata */
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = "account";
attributeRequest.LogicalName = "customertypecode"; //Relationship Type picklist

RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)metaService.Execute(attributeRequest);

/* Cast the attribute metadata to a picklist metadata */
PicklistAttributeMetadata picklist =
(PicklistAttributeMetadata)attributeResponse.AttributeMetadata;

/* set the default value to "customer" (3) */
picklist.DefaultValue = (object)3;

/* update the attribute metadata */
UpdateAttributeRequest updateRequest = new UpdateAttributeRequest();
updateRequest.Attribute = picklist;
updateRequest.EntityName = "account";
updateRequest.MergeLabels = false;

metaService.Execute(updateRequest);

/* Publish the changes */
PublishXmlRequest request = new PublishXmlRequest();

request.ParameterXml = @"<importexportxml>
<entities>
<entity>account</entity>
</entities>
<nodes/>
<securityroles/>
<settings/>
<workflows/>
</importexportxml>";


PublishXmlResponse response =
(PublishXmlResponse)crmService.Execute(request);
}
catch(System.Web.Services.Protocols.SoapException sex)
{
Console.WriteLine(sex.Detail.OuterXml);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

2 comments:

shweta said...

nice article & helpful. However I faced some issues around privileges whereby I am supposed to give this publish access to aperticular security role. Now I have overcome by providing "Publish" privilege to my custom role in CRM.

Gouse said...

Great insights on setting picklist default values! This is a crucial step for streamlining user experience and ensuring data accuracy. Thanks for sharing
Digital Marketing Course In Hyderabad
Digital Marketing Course In Ameerpet