Writing CRUD Operations

  • The XrmLibrary is not meant to replace operations already offered by the CRM 2011 SDK as it is already very well written. The largest benefits of the XrmLibrary are ease of configuration leading to rapid development and built-in tracing/logging functionality via log4net. Some of the operations such as create and retrieve have been extended via extension methods to allow for the use of inline methods leading to less code.
  • In the following code sample, the common operation of creating an account and child contact is shown. Firstly, an IOrganizationService is instantiated via the singleton Services.Instance.OrganizationService property. The account is created and then the child contact is created, with a reference back to the account as the parent customer. Finally, the account is retrieved and updated with the contact as the primary contact. Notice that the create is handled via the CreateRecord extension method while the other operations all use out of the box Microsoft CRM 2011 SDK methods.

using(var service = Services.Instance.OrganizationService)
    // Create an account and obtain the account ID GUID that is generated by Microsoft CRM.
    Guid accountId = service.CreateRecord("account", new[]
                                            new KeyValuePair<string, object>("name", "John Smiths Co.")

    // Create a contact and link it to the parent account.
    Guid contactId = service.CreateRecord("contact",
                                    new KeyValuePair<String, Object>("firstname", "John"),
                                    new KeyValuePair<String, Object>("lastname", "Smith"),
                                    new KeyValuePair<String, Object>("jobtitle", "Manager"),
                                    new KeyValuePair<String, Object>("parentcustomerid", new EntityReference("account", accountId)), 

    // Retrieve the account and set the customer as the primary contact.
    // The XrmLibrary is light to allow for native usage of CRM 2011 SDK objects.
    Entity account = service.Retrieve("account", accountId, new ColumnSet(true));
    account["primarycontactid"] = new EntityReference("contact", contactId);

    // Update the account.

  • Another common integration routine is to pull records from one CRM organization and migrate them to another. Below is another example that shows pulling all contacts named "John" from the first CRM organization and inserting them into the second. Here the XrmLibrary instantiates a service for both the source CRM organization and the target CRM organization. Next a WhoAmI request is made to both organizations and details are printed to the screen. All contacts with first name "John" are then pulled from the source using the XrmLibrary's RetrieveRecords extension method and are then created in the target using a standard IOrganizationService.Create. Again, a standard IOrganizationService.Retrieve could have been used in place if you require more control than RetrieveRecords offers.

// Get a service instance for the first organization in the config file. 
using (var serviceSource = Services.Instance.OrganizationService)
    // Get a service instance for the second organization in the config file (ordinal 1).
    using (var serviceTarget = Services.Instance.GetOrganizationService(1))
        // Perform a WhoAmI request to the source and target instances and print details of the organizations to the console.
        Console.WriteLine(@"**SOURCE** - {0}", serviceSource.WhoAmI().GetDetails());
        Console.WriteLine(@"**TARGET** - {0}", serviceTarget.WhoAmI().GetDetails());

        // Retrieve all contacts with first name "John" from the source CRM organization.
        IEnumerable<Entity> johnContacts = serviceSource.RetrieveRecords("contact", "firstname", "John");

        // Iterate over all the source contacts found and create them in the target CRM organization.
        foreach (Entity johnContact in johnContacts)

There are many more extension methods that are fully documented in the Extension Methods area of the documentation.

Last edited Oct 26, 2011 at 5:59 PM by cchamberlain, version 2


No comments yet.