Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
//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:

...