public static void DataverseConnector()
{
try
{
string conn = @"AuthType=OAuth;Username=abcd@org.onmicrosoft.com;Password=password;Url=https://dorg.crm.dynamics.com;TokenCacheStorePath=c:\temp\MyTokenCache2;LoginPrompt=Auto";
using (ServiceClient serviceClient = new ServiceClient(conn))
{
if (serviceClient.IsReady)
{
// Create an ExecuteTransactionRequest object.
var requestToCreateRecords = new ExecuteTransactionRequest()
{
// Create an empty organization request collection.
Requests = new OrganizationRequestCollection(),
ReturnResponses = true
};
// Create several (local, in memory) entities in a collection.
var listData = new List<string>();
listData.Add("Account 1");
listData.Add("Account 2");
listData.Add("Account 3");
listData.Add("Account 4");
listData.Add("43b1e6dc-8e3a-ee11-bdf4-000d3a0aab51");
// Add a CreateRequest for each entity to the request collection.
foreach (var list in listData)
{
OrganizationRequest organizationRequest = new OrganizationRequest();
Entity accEntity;
accEntity = new Entity("account");
accEntity["name"] = list;
if (list == "43b1e6dc-8e3a-ee11-bdf4-000d3a0aab51")
{
organizationRequest.RequestName = "Update";
accEntity["name"] = "Update record";
accEntity.Id = new Guid("43b1e6dc-8e3a-ee11-bdf4-000d3a0aab51");
}
else
{
organizationRequest.RequestName = "Create";
}
organizationRequest.Parameters.Add("Target", accEntity);
requestToCreateRecords.Requests.Add(organizationRequest);
}
// Execute all the requests in the request collection using a single web method call.
try
{
var responseForCreateRecords =(ExecuteTransactionResponse)serviceClient.Execute(requestToCreateRecords);
// Display the results returned in the responses.
foreach (var responseItem in responseForCreateRecords.Responses)
{
if (responseItem != null)
{
if (responseItem.ResponseName == "Create")
{
Console.WriteLine(responseItem.ResponseName + " with account id as " + responseItem.Results["id"].ToString());
}
else if (responseItem.ResponseName == "Update")
{
Console.WriteLine(responseItem.ResponseName);
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
Console.WriteLine("Create request failed for the account{0} and the reason being: {1}",
((ExecuteTransactionFault)(ex.Detail)).FaultedRequestIndex + 1, ex.Detail.Message);
throw;
}
}
else
{
Console.WriteLine("A web service connection was not established.");
}
}
// Pause the console so it does not close.
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}