In part 1 I gave an overview of how MS CRM entities and attributes, creating entity classes and creating CrmEntityControllers. This blog post will focus on the methods available through the CrmEntityController<T> class.
In the examples below we will assume that the CrmContact class from part 1 exists, so the only thing we need is a CrmControllerFactory and CrmEntityController<T> object:
CrmControllerFactory factory = CrmControllerFactory.Instance();
CrmEntityController<CrmContact> crmContactController = factory.GetEntityController<CrmContact>();
Creating a CRM entity
CrmEntityController<T>.Create(T entity) : Guid
Example:
CrmContact contact = new CrmContact();
contact.FirstName = "John";
contact.LastName = "Smith";
Guid contactGuid = crmContactController.Create(contact);
Reading a CRM entity
Get:
CrmEntityController<T>.Get(Guid entityId) : T
Example:
CrmContact contact = crmContactController.Get(guid);
FindAll:
CrmEntityController<T>.FindAll() : List<T>
CrmEntityController<T>.FindAll(CrmSortingCriteria sorting) : List<T>
Example:
List<CrmContact> contactList = crmContactController.FindAll();
List<CrmContact> sortedContactList = crmContactController.FindAll(new CrmSortingCriteria("firstname", CrmSortOrder.Ascending));
Find:
CrmEntityController<T>.Find(CrmQuery query) : List<T>
CrmEntityController<T>.Find(CrmQuery query, CrmSortingCriteria sorting) : List<T>
CrmEntityController<T>.Find(CrmQuery query, int page, int pageSize, out int totalCount, CrmSortingCriteria sorting) : List<T>
Example:
CrmQuery query = new CrmQuery();
query.AddCondition(new CrmAttributeCriterion("firstname", "John", CrmConditionOperator.Equal));
query.LogicalOperator = CrmLogicalOperator.Or;
query.AddCondition(new CrmAttributeCriterion("firstname", "Sarah", CrmConditionOperator.Equal));
List<CrmContact> contactList = crmContactController.Find(query);
List<CrmContact> sortedContactList = crmContactController.Find(query, new CrmSortingCriteria("firstname", CrmSortOrder.Ascending));
int totalCount;
List<CrmContact> pagedContactList = crmContactController.Find(query, 1, 10, out totalCount, null);
Updating a CRM entity
CrmEntityController<T>.Update(T entity)
Example:
CrmContact contact = crmContactController.Get(guid);
contact.FirstName = "Paul";
crmContactController.Update(contact);
Deleting a CRM entity
CrmEntityController<T>.Delete(Guid guid)
Example:
crmContactController.Delete(guid);
So these are the methods created so far, keep in mind that they might change! One of the things that definitely will change is the CrmQuery class. I’m not quite happy with that yet. So if you have any tips on how to create a good query system, please let me know :)
I might have missed something, but how will this project relate to the new CRM SDK (4.0.12) that has Linq2Crm support?
ReplyDeleteI've contemplated whether I should use QueryExpressions or Linq to query entities. As the Linq2Crm queries are converted to QueryExpressions, I'm a bit concerned about the performance of Linq2Crm. But then on the other hand, most developers are familiar with Linq, so because of that Linq would be better. So far I've stuck to QueryExpressions, but if you have any convincing opinions, I'm happy to consider switching :)
ReplyDelete