Uploading File Attachments to Yammer

Working with attachments to message postings in Yammer has been something that I’ve had a few questions on over time and just recently had a chance to take a look at it.  I decided to blog about it only because there appears to be such paucity of information around folks that have actually done this successfully.  Unfortunately the Yammer developer documentation is skimpier than ever in providing you useful information and examples for getting this done.  So…I’ve again updated my original .NET library for working with Yammer that I described here:  http://blogs.technet.com/b/speschka/archive/2013/10/05/using-the-yammer-api-in-a-net-client-application.aspx.  I’ve modified the MakePostRequest method so that you can pass in a local file name and content type, and it will upload the file to Yammer and add it as an attachment to a message posting.  All this for the unbelievably low price of “free”.  🙂

So to include an attachment with your post in Yammer now, calling the MakePostRequest method looks something like this:

response = MakePostRequest(msg, messageUrl + “.json”, accessToken, “”, “C:\\Users\\speschka\\Desktop\\Yammer Sales Handbook.docx”, “application/vnd.openxmlformats-officedocument.wordprocessingml.document”);

In this case I got the long funky attachment content type from looking at a Fiddler trace while I was figuring this out.  In practice, I haven’t really determined how important it is to get this correct at all.  It doesn’t block the file upload itself, which uses a multipart/form-data content type for the upload.  In terms of getting it to work, what I’ve found the easiest to implement is using the pending attachment method that Yammer describes in their documentation.  This allows me to operate on the file upload(s) separately from posting to a group or whatever.  When you do a pending attachment upload you send the file up to Yammer, and as you might expect, it sends you a big chunk of JSON back to tell you what it did with it.  As with other features in my .NET examples, I wrote up some classes to deserialize the data that Yammer sends back so you can easily refer to properties of the upload.  In this case, when you upload a pending attachment Yammer sends you back an ID, and then you need to include that ID when you make your message post.  Here’s a brief example of adding that to the body that I’m going to post to Yammer:

//upload the file so we can get the attachment ID

YammerFileUpload ya = UploadFile(attachmentName, authHeader, uploadContentType);

//add the attachment info to the form post message body

postBody += “&pending_attachment1=” + ya.id;

The other perfectly delicious part to this whole puzzle is the URL where you upload your files. This part I found NO WHERE in the Yammer documentation, so Fiddler to the rescue again.  Turns out you can send a pending upload to https://files.yammer.com/v2/files and then just add your access token to the query string, i.e. ?access_token=blah.

In any case, this should be enough to get you uploading files to Yammer in most cases, and serve as a code example for other scenarios not covered here (like upload multiple files).  I’ve broken the file upload process itself into a separate method to make it more easily extendible should you need to do so.  Enjoy!



You can download the attachment here:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s