将消息(请求和响应类)与 ExecuteCrmOrganizationRequest 方法结合使用

 

发布日期: 2016年11月

适用于: Dynamics CRM 2015

除了使用 IOrganizationService 之外。Execute 方法,您现在可以使用 CrmServiceClient。用 ExecuteCrmOrganizationRequest 方法以执行 xRM 和 Dynamics 365 消息。 与 Execute 方法相似,ExecuteCrmOrganizationRequest 方法将消息请求类作为参数,并返回消息响应类。 有关可使用 CrmServiceClient.ExecuteCrmOrganizationRequest 方法来执行的消息列表,请参阅 组织服务中的 xRM 消息组织服务中的 CRM 消息

以下代码示例演示了使用 ExecuteCrmOrganizationRequest 方法如何执行消息。

示例 1:CreateRequest 消息

以下代码示例演示了如何使用 CrmServiceClient 执行 CreateRequest 消息。ExecuteCrmOrganizationRequest 方法。 在此示例中,您创建账户,然后在响应对象中显示 ID。

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;
}

示例 2:RetrieveMultipleRequest

以下代码示例演示使用 CrmServiceClient 如何执行 RetrieveMultipleRequest 消息。ExecuteCrmOrganizationRequest 方法。 在此示例中,请执行检索多个请求以提取系统中的所有联系人,并显示这些联系人的全名。

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;
}

另请参阅

将消息(请求和响应类)与 Execute 方法结合使用
使用 XRM tooling 连接到 CRM
使用 XRM 工具执行 CRM 中的操作

© 2017 Microsoft。 保留所有权利。 版权