ExecuteCrmOrganizationRequest メソッドでメッセージを使用する

次のコード サンプルは、ExecuteCrmOrganizationRequest メソッドを使用してメッセージを実行する方法を示しています。

注意

ServiceClient.ExecuteOrganizationRequest 使用することもでき、同じ結果が得られます。

例 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 でアクションを実行

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。