Use XRM tooling to retrieve data
There are many methods available in the CrmServiceClient and ServiceClient classes for retrieving data in Microsoft Dataverse. The following examples demonstrate how you can retrieve a record by ID or FetchXML query.
GetEntityDataById
This method searches for a table by the specified ID. In this sample, we specify null for the field list value to fetch all the columns of the specified table record (account), and then display the name of the retrieved account record.
When using the ServiceClient
class you can find the Get method here: 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
This method searches for the table based on the specified FetchXML
query. In this sample, we retrieve and display the count of all account records in the system.
When using the ServiceClient
class, the query method can be found here: 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;
}
See also
Use XRM Tooling to connect to Dataverse
Use XRM Tooling API to execute actions in Dataverse