Use messages with the ExecuteCrmOrganizationRequest method

Applies to Dynamics 365 (online), version 9.x

In addition to using theIOrganizationService.Execute method, you can now use the CrmServiceClient.ExecuteCrmOrganizationRequest method to execute the xRM and Dynamics 365 Customer Engagement messages. Similar to the Execute method, the ExecuteCrmOrganizationRequest method takes a message request class as a parameter and returns a message response class. For a list of messages that you can execute using the CrmServiceClient.ExecuteCrmOrganizationRequest method, see xRM Messages in the Organization Service and Dynamics 365 Messages in the Organization Service.

The following code samples demonstrate how you can execute messages using the ExecuteCrmOrganizationRequest method.

Example 1: CreateRequest message

The following code sample demonstrates how to execute the CreateRequest message using the CrmServiceClient.ExecuteCrmOrganizationRequest method. In this example, you create an account, and then display the ID in the response object.

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", "<Domain>"),"<Server>", "<Port>", "<OrgName>");  
  
// Verify that you are connected.  
if (crmSvc != null && crmSvc.IsReady)  
{  
    //Display the CRM version number and org name that you are connected to.  
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}",   
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);  
  
    CreateRequest request = new CreateRequest();  
    Entity newAccount = new Entity("account");  
    newAccount.Attributes.Add("name", "Sample Test Account");  
    request.Target = newAccount;  
    CreateResponse response = (CreateResponse)crmSvc.ExecuteCrmOrganizationRequest(request);  
  
    // Display the ID of the newly created account record.  
    Console.WriteLine("Account record created with the following ID: {0}", response.id.ToString());  
}  
else  
{  
    // Display the last error.  
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(crmSvc.LastCrmException.Message);  
    Console.WriteLine(crmSvc.LastCrmException.Source);  
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);  
  
    return;  
}  

Example 2: RetrieveMultipleRequest

The following code sample demonstrates how to execute the RetrieveMultipleRequest message using the CrmServiceClient.ExecuteCrmOrganizationRequest method. In this example, you execute a retrieve multiple request to fetch all the contacts in the system, and display their full name.

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", "<Domain>"),"<Server>", "<Port>", "<OrgName>");  
  
// Verify that you are connected.  
if (crmSvc != null && crmSvc.IsReady)  
{  
    //Display the CRM version number and org name that you are connected to.  
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}",   
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);  
  
    QueryExpression userSettingsQuery = new QueryExpression("contact");  
    userSettingsQuery.ColumnSet.AllColumns = true;  
    var retrieveRequest = new RetrieveMultipleRequest()  
    {  
        Query = userSettingsQuery  
    };  
    EntityCollection EntCol = (crmSvc.ExecuteCrmOrganizationRequest(retrieveRequest) as RetrieveMultipleResponse).EntityCollection;  
    foreach (var a in EntCol.Entities)  
    {  
        Console.WriteLine("Account name: {0} {1}", a.Attributes["firstname"], a.Attributes["lastname"]);  
    }  
}  
else  
{  
    // Display the last error.  
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(crmSvc.LastCrmException.Message);  
    Console.WriteLine(crmSvc.LastCrmException.Source);  
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);  
  
    return;  
}  

See also

Use Messages (Request and Response Classes) with the Execute Method
Use XRM Tooling to connect to Dynamics 365
Use XRM Tooling API to execute actions in Dynamics 365