ExecuteCrmOrganizationRequest メソッドでメッセージを使用する
次のコード サンプルは、ExecuteCrmOrganizationRequest メソッドを使用してメッセージを実行する方法を示しています。
ヒント
ServiceClient.ExecuteOrganizationRequest 使用することもでき、同じ結果が得られます。
アプリケーション コードでの接続文字列の使用については、次の重要な情報をお読みください。
重要
Microsoft では、利用可能な最も安全な認証フローを使用することをお勧めします。 この記事で説明する認証フローは、アプリケーションに対する非常に高い信頼を必要とし、他のフローには存在しないリスクを伴います。 このフローは、マネージド ID など、他のより安全なフローが実行できない場合にのみ使用してください。
例 1: CreateRequest メッセージ
次のコード サンプルは、CrmServiceClient.ExecuteCrmOrganizationRequest メソッドを使用して CreateRequest メッセージを実行する方法を示しています。 この例では、取引先企業を作成してから、応答オブジェクトに ID を表示します。
CrmServiceClient svc = new CrmServiceClient(connectionstring);
// ServiceClient svc = new ServiceClient(connectionstring);
// Verify that you are connected.
if (svc != null && svc.IsReady)
{
var request = new CreateRequest();
var newAccount = new Entity("account");
newAccount.Attributes.Add("name", "Sample Test Account");
request.Target = newAccount;
var response = (CreateResponse)svc.ExecuteCrmOrganizationRequest(request);
// Display the ID of the newly created account record.
Console.WriteLine("Account record created with the following ID: {0}", response.id.ToString());
}
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;
}
例 2: RetrieveMultipleRequest
次のコード サンプルは、CrmServiceClient.ExecuteCrmOrganizationRequest メソッドを使用して RetrieveMultipleRequest メッセージを実行する方法を示しています。 この例では、複数取得の要求を実行し、システム内のすべての取引先担当者をフェッチし、それらの氏名を表示します。
CrmServiceClient svc = new CrmServiceClient(connectionstring);
// ServiceClient svc = new ServiceClient(connectionstring);
// Verify that you are connected.
if (svc != null && svc.IsReady)
{
var userSettingsQuery = new QueryExpression("contact");
userSettingsQuery.ColumnSet.AllColumns = true;
var retrieveRequest = new RetrieveMultipleRequest()
{
Query = userSettingsQuery
};
EntityCollection EntCol = (svc.ExecuteCrmOrganizationRequest(retrieveRequest) as RetrieveMultipleResponse).EntityCollection;
foreach (var a in EntCol.Entities)
{
Console.WriteLine("Account name: {0} {1}", a.Attributes["firstname"], a.Attributes["lastname"]);
}
}
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 ツールを使用して Microsoft Dataverse に接続する
XRM ツール API を使用して Dataverse でアクションを実行