Create a new console application in Visual Studio called Packt.Xrm.Transaction.
Right-click on References and click on Manage NuGet Packages, and search and install Microsoft.CrmSdk.XrmTooling.CoreAssembly.
Include the following using statements in your .cs file:
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Xrm.Tooling.Connector;
Copy and insert the following code in your main function (don't forget to update the connection string):
var connectionString = "AuthType=Office365;[email protected];Password=; Url=https://.crm6.dynamics.com";
var crmSvc = new CrmServiceClient(connectionString);
using (var serviceProxy = crmSvc.OrganizationServiceProxy) { //Create request collection var request = new OrganizationRequestCollection() { new CreateRequest { Target = new Entity("account") { ["name"] = "Packt Account" } }, new CreateRequest { Target = new Entity("contact") { ["firstname"] = "Packt", ["lastname"] = "Contact" } } };
//Create Transaction and pass previously created request collection var requestToCreateRecords = new ExecuteTransactionRequest() { // Create an empty organization request collection. Requests = request, ReturnResponses = true };
//Execute requests within the transaction var responseForCreateRecords = (ExecuteTransactionResponse)serviceProxy.Execute(requestToCreateRecords);
// Display the results of each response. foreach (var responseItem in responseForCreateRecords.Responses) { Console.WriteLine("Created record with GUID {0}", responseItem.Results["id"].ToString()); } }