Building Your Own Push API for O365 (Ingesting Twitter)

Yes, it can be done.  You have to play by the O365 system's rules, but you can do it.  Here's how it can be done…

The high level steps:

  • Find a data source (Twitter, Facebook, Instagram, whatever)
  • Createconfigure an O365 account with access to Azure AD instance for your tenant
  • Ingest the data, transform to format that O365 will accept (Word, PPT, Excel, PDF)
  • Upload the data into O365 (document library)
  • Update the metadata on the doc/item after upload, this includes:
    • Create necessary user
    • Tag the user
    • Create hashtags
    • Share with those users involved
  • Wait up to 15 minutes for your results to display

So let's walk through these steps in a bit more detail shall we?

First Step – get twitter data

This is done by simply using the HttpWebRequest class of .NET and getting the html of a user's feed (provided they have not locked it down, in my case…Christophe ain't indexable when logged in…LOL).  This is a GET request to http://twitter.com/{username

Parse the HTML – I have provided a Util class to help you with this.  ParseValue is super helpful…call me king of html parsing if you will…

You can then use the twitter html response to parse out the tweet feed of the user.  This would include for each tweet:

  • Mentions
  • Text
  • Hashtags

Great, now you have the tweet data into some class structure (such as a hashtable).  Next is to grab a screen shot of all the tweets.  This can be done using the IE Browser control of .NET: 

Bitmap docImage = new Bitmap(width, height);
webBrowser1.DrawToBitmap(docImage, new Rectangle(webBrowser1.Location.X, webBrowser1.Location.Y, width, height));
docImage.Save("c:\temp\" + this.id + ".png");

You can now take that tweet image and import it into a word document using the System.IO.Packaging namespace.

From there, you now need to upload the word document with the tweet image to your O365 instance.  This can be done using the CSOM libraries.

Now that the word doc with the twitter image is loaded, you now need to update the metadata using the CSOM api.  Now this is a bit more advanced in the fact that hashtags become MMS terms and mentions become sharing links:

Sharing an item can be done via some hidden HTML post apis…this ensure that the shared item shows on the other user's feed.

However, if the users doesn't exist…you need to add them to your Azure AD tenant…this can be done by calling an obscure undocumented api…check out the "CreateUser" method.

If you wait a while…UPS will pick up this new Azure AD user and you will be able to tag the user as the creator and modifier of the new word document…errr..umm…tweet.  Which means you might have to run this more than once for each tweet (ah…the life of a lazy Saas app).

In 15 minutes…the document will be indexed properly, the users will see it on their feed and the users' that it is shared with will see it too…Office Graph at its finest…

The code is now posted to codeplex…@williambaer rejoice…however I did not publish the Word doc generation project (OfficeXml) for patent reasons, so you'll have to figure out that part, its not hard, but also not easy:

https://pushapi.codeplex.com

Easy breezy…MVP for another year…boom

CJG 

 

 

Leave a Reply