SOAP allowed me the security I needed as well as a quick response.
If you're new to SOAP, the syntax is XML and here's a good link from W3Schools.
The purpose of this tutorial is to explain how to create a record in MS Dynamics using a SOAP XML message.
Here's the code:
var xml;
xml = "<?xml version='1.0' encoding='utf-8'?>" + "\n\n" + "<soap:Envelope" +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
'<soap:Header>' +
'<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">' +
'<AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">0</AuthenticationType>' +
'<OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">OrganizationName</OrganizationName>' +
'<CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">00000000-0000-0000-0000-000000000000</CallerId>' +
'</CrmAuthenticationToken>' +
'</soap:Header>' +
'<soap:Body>' +
'<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>' +
"<CreateDuplicatesOptionalParameter><value>false</value></CreateDuplicatesOptionalParameter>" +
"<PersistInSyncOptionalParameter><value>false</value></PersistInSyncOptionalParameter>" +
"<entity xsi:type='work'>" +
"<workdescription>" + workText + "</workdescription>" +
"<hours>" + hours + "</hours>" +
"<minutes>" + minutes + "</minutes>" +
"<isbillable>" + billable + "</isbillable>" +
"<total>" + total + "</total>" +
'<accountidname>' + accountIdName + '</accountidname>' +
"<workdate>" + (currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getYear() + "</workdate>" +
"</entity>" +
"</Create>" +
'</soap:Body>' +
'</soap:Envelope>'
xmlhttp.open("POST", "http://localhost:5555/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
xmlhttp.setRequestHeader("SOAPAction", http://schemas.microsoft.com/crm/2007/WebServices/Create);
xmlhttp.setRequestHeader("Content-Length", xml.length);
xmlhttp.send(xml)
var result = xmlhttp.responseXML.xml;
This XML, when properly executed will create a record of "Work" with a workdescription, hours, minutes, isbillable, total, accountidname and workdate. the var result will contain the guid of the newly created record.
Key things to look at:
'<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>'
This line has the keyword Create, notice the difference than a standard RetrieveMultiple you might have seen in the past. This tells the service to create a record. The child nodes are column names within the table. Remember to be sure to at least set the required fields.
xmlhttp.open("POST", "http://localhost:5555/mscrmservices/2007/crmservice.asmx", false);
This line needs to point toward your instance of Dynamics. Change the localhost to suit your needs, you do not need the Organization Name.
xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">OrganizationName</OrganizationName>'
This line needs the OrganizationName of your CRM instance. You can find that after the .com or port number.
Typical Problems:
The two main issues I ran into were a workflow firing off on creation of a record and not setting the correct business required fields. If you do NOT set the fields the service will attempt to create multiples and put in incorrect data. Remember to keep an eye on your table to check for duplicates.
Conclusion:
Once I got past the duplicate issue I found myself really enjoying the flexibility of using SOAP to create a record. Its pretty simple and handles security nicely.
Happy coding!
