I need to programmatically upload PDF files to existing Salesforce records using the C# SOAP API.
I've seen this question as well as countless others but everything I've found either hasn't solved my issue or is in relation to another language or the REST API. All of the C# SOAP API information I'm able to find uses the Attachment object, which seems to not exist in the API. I'm using the Partner WSDL and correctly added it as a Web Reference to my project.
There does exist an EmailFileAttachment object but it's unable to be cast into an sObject to be saved to the Salesforce record.
This is the code I currently have and I'm able to update data fields just fine.
// Login
var Binding = new SforceService();
var loginResult = Binding.login("Username", "Password");
Binding.Url = loginResult.serverUrl;
Binding.SessionHeaderValue = new SessionHeader();
Binding.SessionHeaderValue.sessionId = loginResult.sessionId;
// Create sObject, update data
var obj = new sObject();
obj.type = "tableName";
obj.Id = "objId";
var document = new XmlDocument();
var nodeList = new List<XmlElement>();
var dataNode = document.CreateElement("fieldName");
dataNode.InnerText = "fieldValue";
nodeList.Add(dataNode);
obj.Any = nodeList.ToArray();
// Save data
sObject[] objArray = { obj };
var saveResults = Binding.update(objArray);
However I'm unable to use Attachment or EmailFileAttachment. Attachment doesn't exist in the Salesforce API namespace that I know of. EmailFileAttachment has all the correct fields that I need, but can't be converted to an sObject, so I'm guessing it's not the correct object to be using or that I'm going about using it incorrectly.
// The type or namespace 'Attachment' could not be found.
//var attachment = new Attachment;
var attachment = new EmailFileAttachment();
attachment.id = "objId";
attachment.fileName = "filename.pdf";
attachment.body = fileData; // fileData is byte[]
// Cannot implicitely convert EmailFileAttachment to sObject.
var objArray = new sObject[] { attachment };
Am I missing something to be able to use Attachment, or is there another way to do this?
Edit: I'm using Salesforce C# SOAP API v45.0
EDIT: As per superfell's answer I uploaded the file as an sObject which worked perfectly. The only stipulation is that the Attachment has to be created as a new Salesforce object, and linked to the original object via the ParentId field. Here is the working code below
var atObj = new sObject();
// Set type as "Attachment"
atObj.type = "Attachment";
var document = new XmlDocument();
var nodeList = new List<XmlElement>();
// Set Filename
var nameNode = document.CreateElement("Name");
nameNode.InnerText = "testfile.pdf";
nodeList.Add(nameNode);
// Set ParentId - this is the object id you want to link the attachment to
var idNode = document.CreateElement("ParentId");
idNode.InnerText = "objId";
nodeList.Add(idNode);
// Set File Data - use Base64 string conversion
string encoded = Convert.ToBase64String(fileData); // fileData is byte[]
var dataNode = document.CreateElement("Body");
dataNode.InnerText = encoded;
nodeList.Add(dataNode);
atObj.Any = nodeList.ToArray();
sObject[] objArray = { atObj };
// Use create(), not update()
var saveResults = Binding.create(objArray);