0

I would like to implement Insert or Update Blob Data for ContentVersion. Specifically to retrieve and upload Documill Templates via command line for future use in GitHub actions. How can I do that with standard tools under Windows and how would it look like under Linux?

1 Answer 1

0

This a solution with PowerShell. It is important the the blob is build with the correct number of leading and ending line breaks as shown in this solution. A solution in Bash would look very similar. When finding a solution for bash, the problem with text vs byte-stream should be looked at carefully.

The solution works with the sf cli and works on the selected default org.

param(
[String]$title="Default File Name",
[String]$filename="default.pdf",
[String]$message="Reason for Change message"
)

#Get ContentDocument Id from Title
$contentDocumentId = sf data get record --sobject ContentDocument --where "Title='$title'" --json | jq -r '.result.Id'

#Get Bearer Token & URL for the selected default org.
$bearerToken = sf org display --json | jq -r '.result.accessToken'
$targetOrg = sf org display --json | jq -r '.result.instanceUrl'

#Build Patch Content, the correct number of linebreaks in the blob section is very important.
$pre = @"
--boundary_string
Content-Disposition: form-data; name="entity_content";
Content-Type: application/json

{
    "ContentDocumentId" : "$contentDocumentId",
    "ReasonForChange" : "$message",
    "PathOnClient" : "$filename"
}

--boundary_string
Content-Type: application/octet-stream
Content-Disposition: form-data; name="VersionData"; filename="$filename"


"@

$post='
--boundary_string--'

$fileContent = [System.IO.File]::ReadAllBytes("$filename")

# Combine all parts into a single array of bytes (do not read as text, but as bytes, to prevent errors)
$combinedContent = @()
$combinedContent += [System.Text.Encoding]::UTF8.GetBytes($pre)
$combinedContent += $fileContent
$combinedContent += [System.Text.Encoding]::UTF8.GetBytes($post)

# Write combined content via byte stream
[System.IO.File]::WriteAllBytes("NewContentVersion.json", $combinedContent)

#Create a zip, to be later used as artifact
[System.IO.File]::WriteAllBytes("bak.zip", $fileContent)


$url = "$targetOrg/services/data/v60.0/sobjects/ContentVersion"
$headers = @{
    "Authorization" = "Bearer $bearerToken"
    "Content-Type"  = "multipart/form-data; boundary=boundary_string"
}

Invoke-RestMethod -Uri $url -Method Post -Headers $headers -InFile "NewContentVersion.json"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .