In my canvas app, I can easily post to a chatter feed in this way:

  name: "publisher.getPayload", onData: function (event)
    var post = {};

    post.feedItemType = "LinkPost";
    post.url = url;
    post.urlName = urlName;

      { name: "publisher.setPayload", payload: post });

- this posts a link item.

But how do I post a multisegment item? Apex/SOAP/REST API documentation segments of Salesforce.com have examples of posting such items; but is it possible with a simple Canvas API?

My ultimate goal is to post a text+link+inline image item, as Salesforce doesn't allow to attach preview images to link items (except for some of the whitelisted content providers).

Thank you in advance.


I found the following in Posting to a Chatter Feed using the Cross-Domain XHR client side proxy support.

var sr = JSON.parse('<%=signedRequestJson%>');
// Reference the Chatter user's URL from Context.Links object.
var url = sr.context.links.chatterFeedsUrl+"/news/"+sr.context.user.userId+"/feed-items";
var body = {body : {messageSegments : [{type: "Text", text: "Some Chatter Post"}]}};

    {client : sr.client,
        method: 'POST',
        contentType: "application/json",
        data: JSON.stringify(body),
        success : function(data) {
        if (201 === data.status) {

Of note is how it does a more RAW AJAX POST to via the Canvas API in JavaScript.

With this, the problem is how to do a multipart/form-data POST via the Canvas ajax method. The Chatter API: Examples, Best Practices and Tips links to an example multipart POST.

The source for the Canvas ajax method is on GitHub.

Try switching out the JSON.stringify(body) with the FormData instance.

At a very high level:

  1. Add the first FormData for the normal JSON body. Se the Content-Type to application/json. See Composing multipart/form-data with a different Content-Type on each parts with Javascript
  2. Append the next FormData entry for the attachment with the correct Content-Type.
  3. Use the FormData as the data parameter to the Ajax request.

From Sending multipart/formdata with jQuery.ajax:
I'm aware that a the jQuery ajax method is a very different thing to the canvas ajax method, but it might provide some clues in the right direction around using the FormData.

var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
    data.append('file-'+i, file);

    url: 'php/upload.php',
    data: data,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
  • Hi Daniel, thank you for your post. There are 2 problems here, though: 1. You're basically using the REST API (just through the Canvas API ajax method), not the regular canvas API event flow; 2. The main problem is that if I use this method inside the canvas event flow (publisher.getPayload/publisher.setPayload), I'll get 2 chatter posts. I can surely get away from the regular event flow, but that would mean replicating the Submit/"Post to group"/etc. functionality of the publisher. Not sure what to do here. Aug 15 '16 at 0:09
  • Thank you for the upload part, though :) Aug 15 '16 at 0:11
  • Just in case anyone's interested: github.com/mikebz/mikebz-node-canvas/blob/master/views/… - here's the app that demonstrates the approach. Aug 15 '16 at 0:38
  • It seems that there's no way to make a multi-segment post from within the getPayload/setPayload, so I'm going to reward this answer. Aug 15 '16 at 0:39
  • OK. Glad you found something useful in in :). Aug 15 '16 at 0:44

It seems that currently the only way to make a multi-segment post is to use the REST API. I've created a post on IdeaExchange about supporting multi-segment posts with the regular canvas API, please vote for it:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.