Technical Fun

Archive for June 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:

Holla, CRM Party people, to entertain your greed towards CRM snippets and to keep me from forgetting such a trivial thing, here we go … the adding attachment / note / annotation snippet for CRM 4.0. (In this sample I am binding it to the incident entity).

1 public static bool AddAttachment(string filename, Guid incidentguid, byte[] modal, out string errormessage) 2 { 3 try 4 { 5 string encodedData = Convert.ToBase64String(modal); 6 7 annotation ano = new annotation(); 8 ano.filename = filename; 9 ano.isdocument = new Petrosea.CRM.SD.MetaUtility.CRMWebService.CrmBoolean(); 10 ano.isdocument.Value = true; 11 ano.documentbody = encodedData; 12 ano.mimetype = "application/octet-stream"; 13 ano.objectid = new Lookup(); 14 ano.objectid.type = EntityName.incident.ToString(); 15 ano.objectid.Value = incidentguid; 16 ano.objecttypecode = new EntityNameReference(); 17 ano.objecttypecode.Value = EntityName.incident.ToString(); 18 19 TargetCreateAnnotation target = new TargetCreateAnnotation(); 20 target.Annotation = ano; 21 CreateRequest createrequest = new CreateRequest(); 22 createrequest.Target = target; 23 CreateResponse created = (CreateResponse)crmService.Execute(createrequest); 24 25 errormessage = String.Empty; 26 return true; 27 } 28 catch (Exception ex) 29 { 30 errormessage = ex.Message; 31 return false; 32 } 33 }

Tags: