Technical Fun

Archive for the ‘CRM’ Category

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.

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:

Holla, Ok, I am new to the world of CRM 4.0 Plug-Ins, and even though I wrote many callouts for CRM 3.0, that does not count to the experience. So, I am a flat newbie 🙂 [excuse mode ON].

Here’s the story; I was trying to hook to a post update event of the opportunity entity, and naturally wanting to do something based on the opportunityid I (should have) obtained easily.

Here’s the un-easy part;

Microsoft.Crm.Sdk.DynamicEntity opp = context.PostEntityImages["opportunity"] as Microsoft.Crm.Sdk.DynamicEntity;

and afterwards, I could call the Id (GUID) of the opportunity like this;

Microsoft.Crm.Sdk.Key okey = opp["opportunityid"] as Microsoft.Crm.Sdk.Key; Guid oppid = okey.Value;

Hmm… before I got here, I tried to use and register PreEntityImages, but failed. I tried to use OutputParameters of the context, and failed as well.

So, for post events, I’ll stick to this PostEntityImages thing.

Tags:

Have you ever desired to pull the content of your CRM picklist attributes and show it to the world using other presentation method? No? really? why? hhh… so sad.
But for those of you who bears the same desire as mine, here’s a little help.

Remember that oMds is an instance of CRM Metadata web service.

private PicklistAttributeMetadata getPickListValue(string entityName, string pickListAttributeName)
{
AttributeMetadata amd = oMds.RetrieveAttributeMetadata(entityName,pickListAttributeName);
PicklistAttributeMetadata pl = (PicklistAttributeMetadata)amd;
return pl;
}

As you see, that method will return PicklistAttributeMetadata, which containes Options attribute, that you can iterate later, like this:
Assume that you have an entity named “new_suppliers” and a picklist attribute called “new_supplierstype”.

PicklistAttributeMetadata pam = getPickListValue("new_suppliers","new_supplierstype");
foreach (Option o in pam.Options)
{
lstAttribute.Items.Add(o.Description); //i.e: you got a listbox called lstAttribute and decided to bind the picklist to it.
}

Ok? No? eh? Why ..? hhh .. so sad..

Tags: ,

I wanted to deactivate an entity member and was confused.
Here’s to get you out from the same headache.
Replace ENTITYNAMEHANDLER with your entity’s name.

(note that oCrm is an instance of the Crm Web Service)

SetStateENTITYNAMEHANDLERRequest state = new SetStateENTITYNAMEHANDLERRequest();
state.ENTITYNAMEHANDLERState = ENTITYNAMEHANDLERState.Inactive;
state.ENTITYNAMEHANDLERStatus = 2;
state.EntityId = new Guid(GUID_OF_THE_TARGET_MEMBER);
SetStateENTITYNAMEHANDLERResponse stateSet = (SetStateENTITYNAMEHANDLERResponse)oCrm.Execute(state);

With that, you’re done :), are you? eh? really? huh? …

Tags: ,