データを取得するために XRM ツールを使用する
公開日: 2017年1月
対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online
Microsoft Dynamics 365 でデータを取得するために、 CrmServiceClient クラスには複数のメソッドが用意されています。 次の例は ID または FetchXML クエリでレコードを取得する方法を示します。
GetEntityDataById
このメソッドは、指定された ID でエンティティを検索します。 このサンプルでは、フィールドのリスト値に null を指定して指定されたエンティティ レコード (取引先企業) のすべての属性を取得し、次に取得した取引先企業レコードの名前を表示します。
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
このメソッドは、指定された FetchXML クエリに基づいてエンティティを検索します。 このサンプルでは、システムのすべての取引先企業レコードの数を取得および表示します。
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;
}
関連項目
サンプル: XRM ツール API のクイック スタート
CrmServiceClient コンストラクターを使用した Dynamics 365 への接続
XRM を使用して Dynamics 365 でアクションを実行
Microsoft Dynamics 365
© 2017 Microsoft. All rights reserved. 著作権