Attaching Files to a Salesforce Record

The type of file is typically a Word doc, PDF, xls, etc. Creating and attaching the File is a 3 step process:

  1. Creating the ContentVersion record. This record represents a specific version of a document in Salesforce Files.

  2. Retrieving the ContentDocumentId field value from the newly created ContentVersion record. A ContentDocument record is automatically created for the new ContentVersion record and the ContentDocumentId is also automatically set on the ContentVersion.

  3. Creating the ContentDocumentLink record to associate the File with its related Salesforce record. Note that a ContentDocument record is automatically created for a new ContentVersion record.

The following example creates a new Salesforce Case and then creates a new File and associates it with the Case. Be aware the ContentVersion VersionData property needs to be a byte array. Please Google converting different types of documents to a byte array using C#.

SalesforceSession salesforceSession = GetActiveSession(); //create a new Salesforce Case CaseService caseService = new CaseService(salesforceSession); Case sfCase = new Case(); sfCase.Subject = "Test Case"; sfCase.Status = "New"; sfCase.Origin = "Web"; sfCase.Subject = "Test Case"; SaveResult saveResult = caseService.Insert(sfCase); //Get a file as Byte Array. This example will use a PDF string filePath = @"D:\My_PDF_File.pdf"; Byte[] bytes = System.IO.File.ReadAllBytes(filePath); //Get information about the file System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); //Step 1 - insert the new ContentVersion ContentVersionService contentVersionService = new ContentVersionService(salesforceSession); ContentVersion newFile = new ContentVersion(); // S- Document is located within Salesforce newFile.ContentLocationPicklist = ContentVersion.ContentLocationPicklistValue.S; newFile.VersionData = bytes; newFile.Title = fileInfo.Name; newFile.PathOnClient = fileInfo.Name; saveResult = contentVersionService.Insert(newFile); //Step 2 - get the ContentVersionId from the new ContentVersion object. newFile = contentVersionService.GetByEntityId(saveResult.id, new string[] { ContentVersion.Fields.ContentDocumentId.Name }); //Step 3 - create the ContentDocumentLink for the new Case and ContentVersion ContentDocumentLink contentDocumentLink = new ContentDocumentLink(); contentDocumentLink.ContentDocumentId = newFile.ContentDocumentId; contentDocumentLink.LinkedEntityId = sfCase.Id; // This indicates that the access to the file has "Inferred permissions". That is, the file can be accessed by anyone with access to the related Case. contentDocumentLink.ShareTypePicklist = ContentDocumentLink.ShareTypePicklistValue.I; ContentDocumentLinkService contentDocumentLinkService = new ContentDocumentLinkService(salesforceSession); saveResult = contentDocumentLinkService.Insert(contentDocumentLink);

Updating Files Attached to a Salesforce Record

When updating an existing File, we are actually creating a new ContentVersion record of the ContentDocument record. The following will emulate using the ‘Update New Version’ button on the Salesforce File Page.

//Set up the Salesforce Session and ContentVersionService SalesforceSession salesforceSession = GetActiveSession(); ContentVersionService contentVersionService = new ContentVersionService(salesforceSession); //Get the local file details string filePath = @"D:\TestSalesforceFileUpdated.pdf"; Byte[] bytes = System.IO.File.ReadAllBytes(filePath); System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); //Step 1 - Get the existing ContentVersion that we want to 'update' with an updated local file ContentVersion existingFile = contentVersionService.GetByEntityId(contentVersionId); //Step 2 - Create a new ContentVersion for the updated local file ContentVersion updatedFile = new ContentVersion(); updatedFile.ContentDocumentId = existingFile.ContentDocumentId; updatedFile.VersionData = bytes; updatedFile.Title = fileInfo.Name; updatedFile.PathOnClient = fileInfo.Name; SaveResult saveResult = contentVersionService.Insert(updatedFile);

When viewing the File in Salesforce, it can be seen to be seen to have 2 versions and is linked to both the Case and the User:

Reference

More information about the ContentVersion and ContentDocumentLink objects can be found here:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm