May 21, 2025
Salesforce CMS Documents can be posted through the Connect API CMS Contents endpoint:
The following example code shows how to upload a string variable containing json. In the example, the SalesforceCmsMediaRequest class is based on the request properties outlined on the endpoint page above. The SalesforceCmsMediaResponse class is based on the following documentation:
Besides this, you also need to authorize requests using OAuth as described here:
public async Task<SalesforceResponse> PostCmsMediaFile(string json, string fileName, SalesforceCmsMediaRequest cmsMedia)
{
SalesforceResponse model = new SalesforceResponse();
using (var httpContent = new MultipartFormDataContent("FILE"))
{
byte[] fileBytes = Encoding.UTF8.GetBytes(json);
using (var fileStream = new MemoryStream(fileBytes))
{
fileStream.Position = 0;
var jsonContent = new StringContent(JsonConvert.SerializeObject(cmsMedia), Encoding.UTF8, "application/json");
httpContent.Add(jsonContent, "\"ManagedContentInputParam\"");
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"contentData\"",
FileName = "\"" + fileName + "\""
};
fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
fileContent.Headers.ContentType.CharSet = Encoding.UTF8.WebName;
httpContent.Add(fileContent);
var content = httpContent.ReadAsStringAsync().Result;
var response = await _client.PostAsync(SalesforceConstants.ConnectApi.ContentsPath, httpContent);
model = new SalesforceResponse(response);
}
}
return model;
}