function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
D_M_CD_M_C 

Attach a file to a Chatter Comment via Soap API

I am trying to transfer Chatter posts from one instance to another.  I am running into problems moving attached files within comments (FeedComment items).

 

I realize that the files attach to comments via the RelatedRecordId (mapped to the ContentVersion object) and I am trying to transfer the file by:

1. Get the VersionData from the ContentVersion that is attached to the comment

2. Create the file on the destination environment by writing the file to disk locally and setting the VersionData, PathOnClient (linked to the file that was just created locally), Description, Title

3. Get the ID of the newly created ContentVersion object (returned by the SaveResult array)

4. Set the FeedComment's RelatedRecordID to the new ID created in step 3

5. Upload the comment

 

The result is the comment is created but the file is not found, resulting in "The file is no longer available.".  Am I missing a step in the file linking?

 

 

Note: My file creation process is similar to this - if there is a way to upload a ContentVersion file without creating it locally, I'd happily check that out.

Best Answer chosen by Admin (Salesforce Developers) 
robertjsnellrobertjsnell

When you insert the ContentVersion objects via the API, what are you specifying for the Origin field? (It can be Chatter (H) or Content (C), which is the default if not specified).  I ask because if the files are assigned to Content, then some security rules are enforced with regards to sharing/posting into Chatter Feeds. If you're assigning to Content, or leaving it defaulted,  try specifying it and see if that works.

 

Thanks,

 

Rob

 

 

 

All Answers

ChrisOctagonChrisOctagon

I'm not entirely sure how to do it in Soap, but you can post a file to a comment using the Chatter REST API: http://www.salesforce.com/us/developer/docs/chatterapi/index.htm

 

Make a POST to /chatter/feed-items/feedItemId/comments with Content-Type multipart/form-data, with a binary part named "feedItemFileUpload", a text part called "title" with the file title, and a text part called "text" with your feed item text. Alternately, you can make a regular non-multipart request with the query parameter "contentDocumentId" set to the pre-uploaded content id.

D_M_CD_M_C

Update:

 

I have broken the process down and I have found a different problem.  I am able to upload files as ContentVersions through the API (though, I cannot see the file through the All Files interface within Chatter, I can see them when I query ContentVersion) and I am able to attach files that I have uploaded through the interface to new comments using the API.  I cannot, however, attach files that I have uploaded via the API.

 

I get the following error message when I try to attach an API-uploaded ContentVersion to an API-created FeedComment item:

The Insert failed because: You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.

 

The message doesn't really make sense since I am using the User ID for the file and comment of the admin account I am logged in as to do the API work.

 

Is there another step in creating a ContentVersion file that will make it attachable?

robertjsnellrobertjsnell

When you insert the ContentVersion objects via the API, what are you specifying for the Origin field? (It can be Chatter (H) or Content (C), which is the default if not specified).  I ask because if the files are assigned to Content, then some security rules are enforced with regards to sharing/posting into Chatter Feeds. If you're assigning to Content, or leaving it defaulted,  try specifying it and see if that works.

 

Thanks,

 

Rob

 

 

 

This was selected as the best answer
D_M_CD_M_C

That fixed it.  Thanks for your quick reply!

robertjsnellrobertjsnell

Great glad to hear it's working.

 

Just to clarify when I said 'security rules' for files that belong to Content.  If you publish a ContentDocument / ContentVersion into a library (ContentWorkspace),  it is possible to share that file into Chatter afterwards, in the UI and using the SObject and Connect APIs.  Typically you'd do that by inserting a FeedItem (FeedItemType = ContentPost), or a FeedComment that references the file using its ContentVersionId.  You can also share the file with groups, users and with the whole company by inserting a ContentDocumentLink to attach the file to a specific location. 

 

To do any of those things though, the user performing the operation needs to be a member of the Library with a Library Permission called "Attach or Share Content".  It's mentioned in the docs here: https://login.salesforce.com/help/doc/en/content_workspace_perm_add.htm    This exists to support certain use cases where library administrators may want to prevent sensitive content from being shared into Chatter.

 

This is only relevant if you're using the Salesforce Content application & Libraries. For files added to Chatter directly, this doesn't apply.

AmoraAmora

I can't stress enough how this post is handy. I managed to migrate the chatter data successfully.

Did you manage to migrate the likes against a chatter post or at least the like count?

yvk431yvk431

Hi ,

 

Can any on of you help me in getting the origin of the content version set to 'H'.

I am unable to view the content  version while uploading the files using data loader.

 

Regards,

Prasad Yadala

AmoraAmora

Can you please clarify, what do you mean you can't view the content version?

Is chatter enabled in your organization?

yvk431yvk431

I figured it out, its just the profile permissions , I set the manage chatter permission for the integration profile I am using.

 

--yvk