How to get Attachments from Salesforce using API in C#

Author - Webner
28.04.2021
|
0 Comments
||

Description: The code below shows how we can get Salesforce attachments from various objects and save them to a particular folder on a local disk. This is the complete code to get all attachments saved in all Contact objects in Salesforce.

string _baseFolderPath = “C:\Users\username\Desktop\SFAttachments”;
string sfObject = "Contact";
string parentFolder = "Contacts";
await GetAndSaveAttachments(sfObject, parentFolder);
private static async Task GetAndSaveAttachments(string sfObject, string parentFolder)
{
try
{
var attachmentsResult = await GetAttachmentsByParentId(sfObject);
dynamic attachments = JsonConvert.DeserializeObject(attachmentsResult);
if (attachments["totalSize"] > 0)
{
foreach (var attachment in attachments["records"])
{
string attId = attachment["Id"];
string fileName = attachment["Name"];
string bodyUrl = attachment["Body"];
string parentId = attachment["ParentId"];
string folder = $@"\{parentFolder}\{parentId}\";
// Here I am getting the file content in bytes here
byte[] fileContent = await GetAttachmentFileContent(bodyUrl);
await saveFile(fileContent, folder, fileName);
}
}
}
catch (Exception ex)
{
_logger.Error(ex, $"Exception caught in within {nameof(GetAndSaveAttachments)}");
}
}
public static async Task<string> GetAttachmentsByParentId(string sfObject)
{
try
{
string instanceUrl = "your SF instance url";
string accessToken = "Salesforce access token value";
string queryParam = $"SELECT Id, Name, ContentType, ParentId, body FROM Attachment WHERE ParentId IN (SELECT Id FROM {sfObject})";
string url = instanceUrl + "/services/data/v43.0/query?q=" + queryParam;
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer " + accessToken);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("text/plain", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content;
}
catch (Exception ex)
{
_logger.Error(ex, $"Exception caught in within {nameof(GetAttachmentsByParentId)}");
return null;
}
}
public static async Task<byte[]> GetAttachmentFileContent(string fileBodyUrl)
{
string instanceUrl = "your SF instance url";
string accessToken = "Salesforce access token value";
string url = instanceUrl + fileBodyUrl;
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer " + accessToken);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("text/plain", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.RawBytes;
}
public static async Task saveFile(byte[] content, string subFolder, string fileName)
{
string folderPath = _baseFolderPath + subFolder;
try
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string fileToSave = folderPath+ @"\"+ fileName;
FileStream stream = new FileStream(fileToSave, FileMode.Create, FileAccess.ReadWrite);
stream.Write(content, 0, content.Length);
stream.Close();
_logger.Trace($"New File is created at the path: {fileToSave}");
}
catch (Exception ex)
{
_logger.Error(ex, $"Exception caught within {nameof(saveFile)} for folderPath: {folderPath}");
}
}

Following is the sample of how the downloaded attachments will be saved in your local:

Salesforce

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *