Hi, i have a big issue with Entities attribute values fetching..all the attributes have their own type which are almost all a pain in the ass to cast as String (and i don't talk about Entity References, which will make me perform extra queries just for them..which is totally insane, and a so old fashioned ) But i'm wondering: is there a way so i can avoid all this pain of checking the type of each attribute and perform the proper casting/conversion on it, like some wizard trick that would magically convert my entity attributes list to a Dictionary<string, string> ??? Honestly, i think that this API is 100% pure crap..please prove me wrong and show me that it is a little less crappy than i thought !
*This post is locked for comments
Please follow the method if you want all the fields to be retrieved as string be try like below.
Entity _crmConfig = service.Retrieve("new_crmconfigurations", prop_EntityIdGuid, new ColumnSet("new_emailsubject", "new_emailbody"));
string subject = _crmConfig.Attributes.Contains("new_emailsubject") ? (string)_crmConfig.Attributes["new_emailsubject"] : string.Empty;
string mailbody = _crmConfig.Attributes.Contains("lib_emailbody") ? (string)_crmConfig.Attributes["new_emailbody"] : string.Empty;
string tempbody = mailbody;
string queue_guid_value = string.Empty;
you can create one method which will do that work for you all the time :
public class AttributeComponent
{
public string LogicalName { get; set; }
public string AttributeType { get; set; }
public string Value { get; set; }
}
public static List<AttributeComponent> ConvertAttributesToStringList(Entity entity)
{
List<AttributeComponent> attributes = new List<AttributeComponent>();
string value = string.Empty;
foreach (var attr in entity.Attributes)
{
Type type = attr.Value.GetType();
switch (type.Name)
{
case "EntityReference":
value = ((EntityReference)entity.Attributes[attr.Key]).Name;
attributes.Add(new AttributeComponent { AttributeType = type.Name,LogicalName =attr.Key,Value= value });
break;
case "String":
value = (string)entity.Attributes[attr.Key];
attributes.Add(new AttributeComponent { AttributeType = type.Name, LogicalName = attr.Key, Value = value });
break;
//and so on
}
}
return attributes;
}
}
You might want to look into the Early Bound programming paradigm in which we generate proxy classes beforehand. That enables us to reference all the attributes of an entity by their name with their proper type built-in.
In the SDK there is a utility called CrmSvcUtil that generates these classes.
I personally use a Visual Studio add-on called XrmToolkit to do this, but there are other free tools to help with this:
github.com/.../Early-Bound-Generator
There are other developer add-ons you might find interesting, including Jason Lattimer's:
https://marketplace.visualstudio.com/items?itemName=JLattimer.DynamicsCRMDeveloperExtensions
And here's a blog that compared Early Bound vs. Late Bound performance:
https://woodsworkblog.wordpress.com/2013/02/25/crm-2011-early-binding-vs-late-binding-performance/
It does not matter if it's crap or not - it's simply what you need to learn if you want to work with Dynamics:)
There isn't really any wizard move for that afaik.
Learn the Web API is my advice. Learn Azure and ADAL too if you need to.
Otherwise, you can minimise the amount of code needed by creating workflows within dynamics.
Workflows do the casting for you:
For example, you can create text fields in the CRM for the fields you want to copy over and make them invisible on the form. A workflows can be set to fire on fields changing to populate strings with their attributes.
eg. You have a field called new_someEntity which references an entity object.
A workflow fires on change of this field and copies one of it's fields into a text field string called new_str_someEntity
The form assistant on the right hand side of the workflow editor can help you see what's possible.
In the workflow you're editing, the string field would then look something like this:
Some Entity String: {fieldIwant(relationship (SomeEntity))}
You don't have to write the above declaration as I said. The form assistant does it for you as you click and select the values you want.
But honestly, I recommend you play around with Lattimer's CRM REST Builder, to get the hang of Web API queries:
https://github.com/jlattimer/CRMRESTBuilder/releases
It works fine in my CRM 365 instance.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156