Technical Fun

CRM 4.0: Fetching Picklist Content, and Pass It to the World!

Posted on: June 12, 2008

Holla holla, CRM Party dudes, it’s me again in a very mundane snippet series of CRM 4.0.
This time, we’re gonna grab the content from a picklist attribute and show it to the world! (Well, usually they end up only seeing an asp.net web app, but whatever ..)

Beware, this one’s an overkill scenario, the essence is somewhere within. Seek. 🙂

First things first, I like to create an interface that should build the structure of a picklist object (Why? because .. just because).

1 interface IPicklistObject 2 { 3 int Id {get; set;} 4 string Name {get; set;} 5 }

Then, if an entity got many picklist attributes, we’ll create each class that implement that interface, just like that. (i.e: This SeverityLevel class below).

1 public class SeverityLevel : IPicklistObject 2 { 3 4 #region IPicklistObject Members 5 6 public int Id 7 { 8 get; 9 set; 10 } 11 12 public string Title 13 { 14 get; 15 set; 16 } 17 18 #endregion 19 }

Further more, if you have many picklist-type attributes on your entity, you might want to do mapping externally. My method of choice was a custom xml file that holds to map, like this:
(We’ll need this to pass to the Activator later ..[why??? sshhh ….!])

1 <configuration> 2 <settings> 3 <attributeName> 4 <severitylevel>Full.Name.Of.Your.Concrete.Class.SeverityLevel</severitylevel> 5 </attributeName> 6 </settings> 7 </configuration>

<severitylevel> tag is actually the name of the picklist attribute in crm.
Well of course as a bonus snippet, here’s what I do to read this XML;

1 private static string getMappedValueOf(string attributename) 2 { 3 XmlDocument xDoc = new XmlDocument(); 4 string sFileName = YOUR_XML_FILE_LOCATION; 5 6 if (File.Exists(sFileName) == false) 7 { 8 return string.Empty; 9 } 10 11 try 12 { 13 xDoc.Load(sFileName); 14 XmlNode theNode = xDoc.SelectSingleNode("/configuration/settings/attributeName/" + attributename); 15 if (theNode != null) 16 return theNode.InnerText; 17 18 xDoc = null; 19 return string.Empty; 20 } 21 catch 22 { 23 xDoc = null; 24 } 25 26 return string.Empty; 27 }

Ok, if you are somewhat still following this guide [ I really have no idea why you do that ], you will need to create this method;

1 private static IPicklistObject CreateFrom(string attributename, int key, string name) 2 { 3 Type atttype = Type.GetType(getMappedValueOf(attributename.ToLower()), false); 4 IPicklistObject result = (IPicklistObject)Activator.CreateInstance(atttype); 5 6 result.Id = key; 7 result.Title = name; 8 9 return result; 10 }

Last and final thing, a method that will fetch the picklist attribute using the metadata service (if you’re not like me, this should be the only part you read).

1 public static IList<IPicklistObject> getPickListValue(string entityName, string pickListAttributeName) 2 { 3 RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest(); 4 attributeRequest.EntityLogicalName = entityName; 5 attributeRequest.LogicalName = pickListAttributeName; 6 attributeRequest.RetrieveAsIfPublished = true; 7 8 RetrieveAttributeResponse attributeResponse = 9 (RetrieveAttributeResponse)metaService.Execute(attributeRequest); 10 11 PicklistAttributeMetadata pl = 12 (PicklistAttributeMetadata)attributeResponse.AttributeMetadata; 13 14 IList<IPicklistObject> resultset = new List<IPicklistObject>(); 15 16 foreach (Option o in pl.Options) 17 { 18 resultset.Add(CreateFrom(pickListAttributeName,o.Value.Value,o.Label.UserLocLabel.ToString())); 19 } 20 21 return resultset; 22 }

So at this point, you can just pass this collection of picklist object to a control, (a drop down list in my case), and be happy.

Advertisements
Tags:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: