Versions Compared

Key

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

...

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.

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);

...