function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Retrieve attachment sample in

Most samples have C++ documentation. Is there a sample code showing how to retrieve an attached document from a given parent Id. Specifically how to download and convert the body from base64.


Hi there, I also get the problem with downloading attachment in .NTET environment and seems no one is replied my posting before, I need this solution badly, is there any moderator or developer out there know how to do this?


I hope someone can answer the question.
If you use the enterprise WSDL, then .NET will automatically convert from the base64 string to the array of bytes that are in the attachment. If you're using the partner WSDL, then you need to call Convert.FromBase64String to do the conversion. Here a quick VB.NET sample with the partner API to download an attachment to a disk file.

Dim stub As New sforce.SforceService
Dim lr As sforce.LoginResult
lr = stub.login("your_login", "your_password")
stub.SessionHeaderValue = New sforce.SessionHeader
stub.SessionHeaderValue.sessionId = lr.sessionId
stub.Url = lr.serverUrl

Dim att(1) As sforce.sObject
Dim id(1) As String
id(0) = "00P30000000kxHe"
att = stub.retrieve("Body, Name", "Attachment", id)
Dim data() As Byte
data = Convert.FromBase64String(att(0).Any(0).InnerText)

Dim fn As String
fn = "c:\dl_" + att(0).Any(1).InnerText
Dim f As New System.IO.FileStream(fn, IO.FileMode.CreateNew)
f.Write(data, 0, data.Length)

Console.WriteLine("Created Attachment " + fn)