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
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기