データを取得するために XRM ツールを使用する
Microsoft Dataverse でデータを取得するには、CrmServiceClient と ServiceClient クラスには複数のメソッドが用意されています。 次の例は ID または FetchXML クエリでレコードを取得する方法を示します。
アプリケーション コードでの接続文字列の使用については、次の重要な情報をお読みください。
重要
Microsoft では、利用可能な最も安全な認証フローを使用することをお勧めします。 この記事で説明する認証フローは、アプリケーションに対する非常に高い信頼を必要とし、他のフローには存在しないリスクを伴います。 このフローは、マネージド ID など、他のより安全なフローが実行できない場合にのみ使用してください。
GetEntityDataById
このメソッドは、指定された ID でテーブルを検索します。 このサンプルでは、フィールド リスト値に null を指定して、指定したテーブル レコード (アカウント) のすべての列を取り込み、取得したアカウント レコードの名前を表示します。
ServiceClient
クラスを使用する場合、次でメソッドを見つけることができます: 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
このメソッドは、指定された FetchXML
クエリに基づいてテーブルを検索します。 このサンプルでは、システムのすべての取引先企業レコードの数を取得および表示します。
ServiceClient
クラスを使用する場合、次でクエリ メソッドを見つけることができます: 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;
}
参照
XRM ツールを使用して Dataverse に接続する
XRM ツール API を使用して Dataverse でアクションを実行