Use messages with the ExecuteCrmOrganizationRequest method

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

Note

You could also use ServiceClient.ExecuteOrganizationRequest to obtain the same results.

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 svc = new CrmServiceClient(connectionstring);  
// ServiceClient svc = new ServiceClient(connectionstring); 

// Verify that you are connected.  
if (svc != null && svc.IsReady)  
{  
    var request = new CreateRequest();  
    var newAccount = new Entity("account");  
    newAccount.Attributes.Add("name", "Sample Test Account");  
    request.Target = newAccount;  
    var response = (CreateResponse)svc.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}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.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 svc = new CrmServiceClient(connectionstring);  
// ServiceClient svc = new ServiceClient(connectionstring); 

// Verify that you are connected.  
if (svc != null && svc.IsReady)  
{  
  
    var userSettingsQuery = new QueryExpression("contact");  
    userSettingsQuery.ColumnSet.AllColumns = true;  
    var retrieveRequest = new RetrieveMultipleRequest()  
    {  
        Query = userSettingsQuery  
    };  
    EntityCollection EntCol = (svc.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}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.LastCrmException.StackTrace);  
  
    return;  
}  

See also

Use XRM Tooling to connect to Microsoft Dataverse
Use XRM Tooling API to execute actions in Dataverse