Compartir a través de


Utilizar los útiles de XRM para recuperar datos

Existen muchos métodos disponibles en las clases CrmServiceClient y ServiceClient para recuperar datos en Microsoft Dataverse. Los siguientes ejemplos demuestran cómo puede recuperar un registro por ID o consulta FetchXML .

GetEntityDataById

Este método busca una tabla por el identificador especificado. En este ejemplo, especificamos NULL para el valor de la lista de campos para obtener todas las columnas del registro de tabla especificado (cuenta) y luego mostrar el nombre del registro de cuenta recuperado.

Al usar la clase ServiceClient, puede encontrar el método Get aquí: QueryExtensions.GetEntityDataById

CrmServiceClient svc = new CrmServiceClient(connectionstring); 
// ServiceClient svc = new ServiceClient(connectionstring); 
  
// Verify that you are connected.  
if (svc != null && svc.IsReady)  
{  
    Dictionary<string, object> data = svc.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}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.LastCrmException.StackTrace);  
  
    return;  
}  
  

GetEntityDataByFetchSearchEC

Este método busca la tabla en función de la consulta FetchXML especificada. En este ejemplo, recuperamos y presentamos el recuento de todos los registros de cuenta del sistema.

Al usar la clase ServiceClient, puede encontrar el método de consulta aquí: QueryExtensions.GetEntityDataByFetchSearch

CrmServiceClient svc = new CrmServiceClient(connectionstring);
// ServiceClient svc = new ServiceClient(connectionstring);  
  
// Verify that you are connected.  
if (svc != null && svc.IsReady)  
{   
    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}", svc.LastCrmError);  
  
    // Display the last exception message if any.  
    Console.WriteLine(svc.LastCrmException.Message);  
    Console.WriteLine(svc.LastCrmException.Source);  
    Console.WriteLine(svc.LastCrmException.StackTrace);  
  
    return;  
}  

Consulte también

Use útiles de XRM para conectarse a Dataverse
Usar herramientas de API XRM para ejecutar acciones en Dataverse

Nota

¿Puede indicarnos sus preferencias de idioma de documentación? Realice una breve encuesta. (tenga en cuenta que esta encuesta está en inglés)

La encuesta durará unos siete minutos. No se recopilan datos personales (declaración de privacidad).