Okay, I got it to work. Not really sure why yet, but will investigate further.
I have a global static object:
public static OrganizationServiceProxy _serviceProxy;
In my calling routine, I set that object:
var serverConnect = new ServerConnection();
var config = serverConnect.GetServerConfiguration();
_serviceProxy = ServerConnection.GetOrganizationProxy(config);
Then I call my method:
At the create part of my CreateLead() method, what I had was:
using (_serviceProxy)
{
_serviceProxy.EnableProxyTypes();
var guid = _serviceProxy.Create(lead);
AttachEmail(oMailItem, guid);
MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
"Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
With that code, it would create the lead just fine, but it would fail creating anything else. Even changing the code to:
using (_serviceProxy)
{
_serviceProxy.EnableProxyTypes();
Contact emailContact = new Contact
{
FirstName = "Nancy",
LastName = "Anderson",
EMailAddress1 = "nancy@contoso.com"
};
var _contactId = _serviceProxy.Create(emailContact);
var guid = _serviceProxy.Create(lead);
AttachEmail(oMailItem, guid);
MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
"Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
With that code, it would FAIL creating the contact, SUCCESSFULLY create the lead, and then FAIL again creating the email.
However, when I changed the code to:
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
_serviceProxy.EnableProxyTypes();
Contact emailContact = new Contact
{
FirstName = "Nancy",
LastName = "Anderson",
EMailAddress1 = "nancy@contoso.com"
};
var _contactId = _serviceProxy.Create(emailContact);
var guid = _serviceProxy.Create(lead);
AttachEmail(oMailItem, guid);
MessageBox.Show("Lead: '" + fName + " " + lName + "' sucessfully added to CRM.",
"Lead Created Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Everything is created just fine with no errors!
I don't know why, but it only seems to work if I re-initialize the _serviceProxy object. And if not, I don't understand why all 3 create calls wouldn't fail. Why would it fail on the first one (contact), succeed on the second (lead) and then fail again on the third (email)?
Strange and kinda sucks from a performance standpoint because every time I have to re-initialize that proxy, it takes time... Especially for a user connecting to the IFD over a slower connection. I haven't tried taking the using statement out of the equation yet. Perhaps eliminating that and just manually calling the Dispose() method in a Finally block would work.
Thanks all for the suggestions!
Nick