There are many benefits of using early-binding when coding against CRM. There are times however when you need to fall back to late-binding, for example when you need to query relationships, or when you need to call Retrieve to optimise performance (as this service call allows you to load only selected fields of a record).
Rather than using literal string, you can use the nameof operator in C# 6 to get the attributes and relationship names. This works because the properties of the early-binding classes are generated based on the attributes and relationship names in CRM.
For example, to get the AccountNumber field of an Account record:
var client = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString); //Remember to convert to lowercase. var accountNumberAttributeName = nameof(Account.AccountNumber).ToLower(); var entity = client.Retrieve(Account.EntityLogicalName, Guid.NewGuid(), new ColumnSet(accountNumberAttributeName)); var accountNumber = entity.ToEntity<Account>().AccountNumber;
And there you have it: late-binding with all the early-binding zen!