使用 XRM tooling 检索数据

 

发布日期: 2017年1月

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

CrmServiceClient 类有多种方法可以在 Microsoft Dynamics 365 中检索数据。 以下示例演示了如何按照 ID 或 FetchXML 查询检索记录。

GetEntityDataById

此方法通过指定的 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);

    Dictionary<string, object> data = crmSvc.GetEntityDataById("account", <Account_ID>, null);
    foreach (var pair in data)
    {
        if (pair.Key == "name")
        {
            Console.WriteLine("Name of the account is {0}", 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;
}

GetEntityDataByFetchSearchEC

此方法基于指定的 FetchXML 查询搜索实体。 在此示例中,我们检索并显示系统中所有客户记录的总数。

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

    string fetchXML = 
        @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' returntotalrecordcount='true' >
            <entity name='account'>
              <attribute name='accountid' />
            </entity>
        </fetch>";
    var queryResult = crmSvc.GetEntityDataByFetchSearchEC(fetchXML);
    if (queryResult != null)
    {
        Console.WriteLine(String.Format("Account Records Count : {0}", queryResult.TotalRecordCount));
    }
}
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。 保留所有权利。 版权