by Daniel Halan
24. June 2010 00:29
When working with Microsoft CRM data-types using web services there is no constructor parameters due to limitations of the WSDL code generation. This leads to few lines of code each time one want to create a variable of any CRM type.
This can be optimized using a Factory class where you encapsle the creation of the desired class. But one even cleaner way is to create a partial class and define "implicit" operators between CRM data-type and .NET data-type.
Here is an example on how it works.
Standard way of assigning a .NET DateTime to CRM DateTime,
CrmDateTime cdt = new CrmDateTime();
cdt.Value = DateTime.Today.ToString("s");
Using implicit declaration,
CrmDateTime cdt = DateTime.Now;
and back,
DateTime dt = cdt;
The code behind this,
public partial class CrmDateTime {
public CrmDateTime(DateTime dt) {
this.Value = dt.ToString("s");
}
public static implicit operator CrmDateTime(DateTime dt) {
return new CrmDateTime (dt);
}
public static implicit operator DateTime(CrmDateTime dt) {
return DateTime.Parse(dt.Value);
}
}
by Daniel Halan
17. December 2009 02:05
Validation of Social Security Number (Personnummer) or the Organisation Number is very common validation when working with business systems, here is a small method to validate the Swedish identification numbers. If you by any chance have done a similar check for your country social security number, would be nice if you post a link to your site or send the code and I will update this post.
IdNrCheck.zip (832,00 bytes)
by Daniel Halan
16. November 2009 23:17
Generics is a very nice feature of C# language, and can be used to simplify the CRM Web Service method calls. One of my responsibilities at Logica is to develop a framework for CRM development, and one of the main classes in this framework is called CrmSystem, it wraps the CrmService methods among other things. Using generics one can then type,
account acc = CrmSystem.Retrieve<account>(myAccountId);
also we use a special NameValue class CrmConditions to one of our Execute overloads, here is how it can look
List<account> acc = CrmSystem.Execute<account>(new CrmCondition("emailaddress1", "daniel@logica.com"));
This would retrieve all accounts that got the email "daniel@logica.com"
Here is one of the overloads for the Retrive Method that uses Generics, it calls an other overload that does the actual call to CRM Web Service using the EntityName string that we get thru typeof(T).Name
public T Retrieve<T>(Guid id, params string[] columnSet) where T : BusinessEntity {
return (T)Retrieve(typeof(T).Name, id, new ColumnSet(columnSet));
}
Hope this gives some inspiration for your own CrmApi wrappers :)