Use messages with the ExecuteCrmOrganizationRequest method
The following code samples demonstrate how you can execute messages using the ExecuteCrmOrganizationRequest method.
Note
You could also use ServiceClient.ExecuteOrganizationRequest to obtain the same results.
Example 1: CreateRequest message
The following code sample demonstrates how to execute the CreateRequest message using the CrmServiceClient.ExecuteCrmOrganizationRequest method. In this example, you create an account, and then display the ID in the response object.
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;
}
Example 2: RetrieveMultipleRequest
The following code sample demonstrates how to execute the RetrieveMultipleRequest message using the CrmServiceClient.ExecuteCrmOrganizationRequest method. In this example, you execute a retrieve multiple request to fetch all the contacts in the system, and display their full name.
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;
}
See also
Use XRM Tooling to connect to Microsoft Dataverse
Use XRM Tooling API to execute actions in Dataverse
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기