Creating Chatter Records with an Attachment

Creating a Chatter record with an attachment add the attachment to the Chatter record rather than the using the conventional Attachment record as outlined here.  Also, the byte array for the attachment must be converted to a base 64 string for the ContentData field of the FeedItem.


GenericSalesforceEntity feedItem = new GenericSalesforceEntity("FeedItem");

//The ParentId is the ID of the sObject that the Feed Item is attached to. In this case it is a Contact ID.
contactFeed.InternalFields.SetField("ParentId", "0039000000Msoqj");
contactFeed.InternalFields.SetField("Body", "Some chatter text");

//Attach a file 
byte[] bytes = System.IO.File.ReadAllBytes("MyPDF.pdf");

contactFeed.InternalFields.SetField("ContentData",
Convert.ToBase64String(fileChatter.FileBytes));


contactFeed.InternalFields.SetField("MyPDF.pdf ", fileChatter.FileName);

GenericSalesforceService chatterContactService = new GenericSalesforceService(GetSalesforceSession, "FeedItem");
SaveResult saveResult = chatterContactService.Insert(feedItem);