Scenario : we have a guid for an incident and we have a datetime string and we need to update this incident with the new datetime. The only problem is : this datetime field is not a standard attribute for the incident entity. So we need Dynamic Entities. The standard code in the SDK gives us an example of retrieving the DynamicEntity (little modification for my incidents) :
ColumnSet cols = new ColumnSet(EntityName.incident.ToString());
cols.AddColumn("incidentid");
cols.AddColumn("xxx_incidenthandledate");
TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
targetRetrieve.EntityName = EntityName.incident.ToString();
targetRetrieve.EntityId = incidentGuid;
RetrieveRequest retrieve = new RetrieveRequest();
retrieve.Target = targetRetrieve;
retrieve.ColumnSet = cols;
retrieve.ReturnDynamicEntities = true;
RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
DynamicEntity theIncident = (DynamicEntity)retrieved.BusinessEntity;
After this we can update the attribute value of the incident with the new datetime :
CrmDateTimeProperty theNewIncidentHandleDate = new CrmDateTimeProperty();
theNewIncidentHandleDate.Name = "xxx_incidenthandledate";
theNewIncidentHandleDate.Value = new CrmDateTime( string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:s}", theNewDateTime));
PropertyCollection propcol = new PropertyCollection();
propcol.Add(theNewIncidentHandleDate);
theIncident.Properties = propcol;
TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();
updateDynamic.Entity = theIncident;
UpdateRequest update = new UpdateRequest();
update.Target = updateDynamic;
UpdateResponse updated = (UpdateResponse)service.Execute(update);
The only problem is : the SoapException I got…

Apparently, I needed to ad my incidentid to the PropertyCollection. Why? Because the webservice method needs to know which incident we need to update…
Yes… I was that stupid. I was looking at that freakin SoapException for about half an hour. Until my colleague Maarten van Sambeek saved me with just one line of code :
propcol.Add(theIncident.Properties.OfType().First());
We could go and build a new KeyProperty, add the name and the value of the incidentid and add it to the PropertyCollection.
What we do is use Linq to get the first Property of type KeyProperty. Yes, that happens to be the incidentid
Et voila : a nicely working update of a CrmDateTimeProperty for a DynamicEntity!
Like this:
Like Loading...