Partilhar via


Uso da ferramenta XRM para recuperar dados

 

Publicado: janeiro de 2017

Aplicável a: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Há vários métodos disponíveis na classe CrmServiceClient para recuperar dados do Microsoft Dynamics 365. Os exemplos a seguir demonstram como recuperar um registro pelo ID ou pela consulta FetchXML.

GetEntityDataById

Este método pesquisa uma entidade pelo ID especificado. Neste exemplo, especificamos null para o primeiro valor da lista de campo para buscar os atributos do registro da entidade (conta) e exibimos o nome do registro da conta recuperado.

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

Este método pesquisa a entidade com base na consulta FetchXML especificada. Neste exemplo, recuperamos e exibimos a contagem de todos os registros da conta do sistema.

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

Confira Também

Exemplo: Início rápido para API de ferramentas XRM
Os os construtores para conectar ao Dynamics 365
Usar as ferramentas do XRM para executar ações no Dynamics 365

Microsoft Dynamics 365

© 2017 Microsoft. Todos os direitos reservados. Direitos autorais