Freigeben über


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: https://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!

 

 

YammerCrawlSyncWithFileUpload.zip

Comments

  • Anonymous
    January 01, 2003
    The comment has been removed
  • Anonymous
    January 01, 2003
    thanks
  • Anonymous
    July 13, 2014
    Evolving Office 365 plans for small and midsized businesses Details about new plans here which also includes
  • Anonymous
    July 14, 2014
    Hi Steve
    Just for your information, here a Yammer C# API on Codeplex, based on what you did
    https://yammerapisdk.codeplex.com.
    I will implement what you did for the upload functionnnality.
  • Anonymous
    July 17, 2014
    Hi Steve
    Do you konw about "https://www.yammer.com/api/v1/pending_attachments"? I can use this restful api to upload an attchement file, but when I use this api to upload a new version for an existed attachment , I failed. So, can you help me? Thx.
  • Anonymous
    January 08, 2015
    m88 : http://m88en.com
    M88.com offer online sports games Asia, Sports Betting Asia, Sports Betting Sites Asia.
    m88asia : http://m88en.net
    Link to M88BET phone: m88en.com. – Register and Open Betting Account and Membership M88BET.
    m88bet : http://www.linkm88vip.com
    MANSION88 the house is one of the largest and most prestigious. Appeared quite early in the Asian market, the so-MANSION88 currently attracts more players.
    link m88 : http://m88wiki.com
    Home the M88 is the official sponsor of the football club in the Premier League
    Wish you happy with the new M88
    m88 casino online : http://m88free.com