Поделиться через


Use messages (request and response classes) with the ExecuteCrmOrganizationRequest method

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In addition to using the IOrganizationService.Execute method, you can now use the CrmServiceClient.ExecuteCrmOrganizationRequest method to execute the xRM and Dynamics 365 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 CrmServiceClient constructors to connect to Dynamics 365
Use XRM tooling to execute actions in Dynamics 365

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright