在C#中上载Salesforce的文件附件

时间:2013-08-27 21:45:51

标签: c# salesforce soap-client

我已经看到this example在Java中构建并向Salesforce发送附件,但是如何在C#中完成?

编辑 - 我也使用this page作为参考,但我仍然不知道如何完成我尝试创建和保存附件的最后一部分。

SoapClient client = new SoapClient();
LoginResult lr = client.login(new LoginScopeHeader(), username, password);

FileInfo fileInfo = new FileInfo(myFileLocation);
FileStream stream = File.OpenRead(myFileLocation);
byte[] byteArray = new byte[fileInfo.Length];
stream.Read(byteArray, 0, byteArray.Length);

Attachment attachment = new Attachment();
attachment.Body = byteArray;
attachment.Name = myFileName + ".txt";
attachment.IsPrivate = false;

SaveResult saveResult = client.create(new sObject[] { attachment })[0];

1 个答案:

答案 0 :(得分:1)

一般来说看起来是正确的。有以下变化。

调用login后,您需要将生成的SessionId和ServerUrl分配给客户端。

SoapClient client = new SoapClient();
LoginResult lr = client.login(new LoginScopeHeader(), username, password);
client.SessionHeaderValue = new SforceService.SessionHeader();
client.SessionHeaderValue.sessionId = li.sessionId;
client.Url = loginResult.serverUrl;

您应该检查SaveResult以查看记录是否已创建以及新ID是什么。

//...
SaveResult saveResult = client.create(new sObject[] { attachment })[0];
if (saveResult .success){ 
    // saveResult.id contains id of newly created attachment
} else {
    //saveResult .errors[0] contains reason why attachment couldn't be created.
}