使用 XRM 工具更新数据

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

CrmServiceClient 类中有两种方法可用于更新 Microsoft Dynamics 365 中的数据:UpdateEntityUpdateStateAndStatusForEntity

使用 XRM Tooling API 更新操作需要一个数据负载。 数据负载执行对 Dictionary<string, CrmDataTypeWrapper> 对象的窗体。CrmDataTypeWrapper 用来通知接口您正引用的数据点需要何种处理。

UpdateEntity

除设置记录的多个或一个状态之外,该标记方法可以更新 Dynamics 365 中的所有记录。 若要使用它,您需要知道以下信息:要更新实体的架构名称,要更新实体的主键字段,要更新记录的 GUID,以及更新数据的数据负载数组。

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

    // Update the account record
    Dictionary<string, CrmDataTypeWrapper> updateData = new Dictionary<string, CrmDataTypeWrapper>();
    updateData.Add("name", new CrmDataTypeWrapper("Updated Sample Account Name", CrmFieldType.String));
    updateData.Add("address1_city", new CrmDataTypeWrapper("Boston", CrmFieldType.String));
    updateData.Add("telephone1", new CrmDataTypeWrapper("555-0161", CrmFieldType.String)); 
    bool updateAccountStatus = crmSvc.UpdateEntity("account","accountid",_accountId,updateData);

    // Validate if the account record was updated successfully, and then display the updated information
    if (updateAccountStatus == true)
    {
        Console.WriteLine("Updated the account details as follows:");
        Dictionary<string, object> data = crmSvc.GetEntityDataById("account", accountId, null);
        foreach (var pair in data)
        {
            if ((pair.Key == "name") || (pair.Key == "address1_city") || (pair.Key == "telephone1"))
            {
                Console.WriteLine(pair.Key.ToUpper() + ": " + pair.Value);
            }
        }
    }
}
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;
}

UpdateStateAndStatusForEntity

此方法来设置 Dynamics 365 中的记录状态。 例如,所有记录通常以“开启”状态启动。 状态的名称根据记录类型甚至开发人员的选择而更改。 例如,报价单有若干可能的状态,“草稿”、“可用”、“关闭”、“丢单”、“赢单”。

提示

您可以使用 SDK 中的 SDK\SampleCode\CS\HelperCode 文件夹中的 OptionSets.cs 文件,下载程序包以查看适用于各种 Dynamics 365 实体的全局选项集。 有关全局选项集的详细信息,请参阅 配置全局选项集

更新实体的状态需要您了解目标状态和状态(按名称或 ID)。 查询实体的元数据和注视状态和状态字段,都可以找到名称和 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);

    // Here are the state and status code values
    // statecode = 1 ( Inactive ) 
    // statuscode = 2 ( Inactive ) 

    crmSvc.UpdateStateAndStatusForEntity("account" , accountId , 1 , 2 );

    // the same command using the second form of the method
    crmSvc.UpdateStateAndStatusForEntity("account" , accountId , "Inactive" , "Inactive");
}
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;
}

另请参阅

示例:XRM Tooling API 快速入门
使用 CrmServiceClient 构造函数连接到 Dynamics 365
使用 XRM 工具执行 Dynamics 365 中的操作
使用属性元数据

Microsoft Dynamics 365

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