In Part 1 of this series, I introduced you to the CloudTopia app.  In Part 2 we’re going to look at some of the work we did with Open Graph items in CloudTopia.

Here's some quick links to the whole series:

  1. Intro
  2. Open Graph
  3. Web API
  4. Azure Integration
  5. Office 365 Integration
  6. Cortana, Speech and Windows Phone Integration

As I described in Part 1, Yammer Open Graph (OG) items are used in CloudTopia in a couple of different ways:  1) to provide a forum for discussion for the team working on an event and 2) as a way to bring in external discussions from Twitter to the OG item so the team can get a good sense of the external buzz that’s happening around their event…like #yammerofjuly (inside joke for those of you who have been following me on Twitter).  As I also described in Part 1, I covered the basic details of working with OG items in a previous post here:  http://blogs.technet.com/b/speschka/archive/2014/05/29/using-yammer-open-graph-in-net.aspx.  I won’t be covering that all over again, but I will be covering some of the other implementation details and things you should be aware of when working with your own OG items.  And that’s really the point of this entire series – not how to write this application per se, but cover some general purpose implementation details and things you should be aware of.

Okay, so we’ll use the blog post above as a starting point for working with OG items, now let’s look at some of the implementation details.  In my previous posts on OG I talked about the object model that I created over an OG item and how to use it to create a new OG item in Yammer.   For review, here’s what my object model looks like:

 

To use it you need to send a chunk of JSON to Yammer to create the OG item.  What I did to facilitate this is let you create an OG item using my object model, and then when you call the ToString() method I’ve overridden that so that it produces the JSON you need.  With that in hand you can use one of the methods I included in my original Yammer and .NET posting to create the item, by calling the MakePostRequest method.  The net of this is that your code looks pretty straightforward:

YammerGraphObject go = new YammerGraphObject();

go.Activity.Action = "create";

 

go.Activity.Actor = new YammerActor("Steve Peschka", "speschka@yammo.onmicrosoft.com");

 

go.Activity.Message = "This is the discussion page for the " + eventName + " event on " + eventDate;

 

go.Activity.Users.Add(new YammerActor("Anne Wallace", "annew@yammo.onmicrosoft.com"));

go.Activity.Users.Add(new YammerActor("Garth Fort", "garthf@yammo.onmicrosoft.com"));

 

YammerGraphObjectInstance jo = new YammerGraphObjectInstance();

jo.Url = Url;

jo.Title = eventName; 

jo.Description = "This is the discussion page for the " + eventName + " event on " + eventDate; 

jo.Image = "https://socialevents.azurewebsites.net/images/eventplanning.png";

jo.Type = "document";

 

go.Activity.Object = jo;

 

string postData = go.ToString();

 

string response = MakePostRequest(postData, graphPostUrl, yammerAccessToken, "application/json");

 

The other thing I mentioned in part 2 of that series on OG items is how important it is to have the OG ID; it is required whenever you want to read from or write to the discussions for the OG.  As I mentioned in that post there are basically three ways to get the OG ID:

  1. Capture it when the OG item is created.  You’ll get some JSON back if the create is successful and we can use that to extract out the ID of the newly created item.

  2. Search for an OG item.  You can search using the Url property of the OG item; however this only returns results if there is at least one discussion item created for the OG.  This is another reason why the CloudTopia app creates an initial discussion item when creating the OG.

  3. Do a “fake” update to the OG item.  What I mean by fake is that I just pass in my own username and email address as the actor, I change the Action to “follow”, and I set the Private property of the Activity to true.  When I do that I get back the same chunk of JSON as I do when I create an OG, so again I can extract out the ID from there.

 

In the case of CloudTopia I simply capture the ID at the time I create the item; that’s clearly the best option if you can do so.  In CloudTopia I take the ID of the newly created OG item and I save it to SQL Azure so I can use it later when creating new discussion items for the OG based on tweets I found for the event.  To extract the ID I built another class to serialize the JSON into and it looks like this:

 

 

Using it is quite simple:

//create the OGO

string response = MakePostRequest(postData, graphPostUrl, yammerAccessToken, "application/json");

 

if (!string.IsNullOrEmpty(response))

{

      YammerGraphObjectItem gi = JsonConvert.DeserializeObject<YammerGraphObjectItem>(response);

}

Finally, remember that you always need the OG ID when reading or writing discussion items for it.  Also, the Url to use to read OG discussion items is currently undocumented “officially”, but I have covered all of this in greater detail in part 2 of my series on working with Yammer Open Graph from .NET (http://blogs.technet.com/b/speschka/archive/2014/05/29/using-yammer-open-graph-in-net-part-2.aspx).

Now let’s bring all the discussion of this back around to a concrete example – what we did in the CloudTopia app.  Remember that we create a new OG item when a new o365 site is created, and we use the Url for the new o365 site as the key for the OG item.  Here’s what that code looks like in CloudTopia:

//NOTE:  WILL EXPLAIN MORE OF THIS CODE

//IN A SUBSEQUENT PART OF THIS SERIES

//create a new site

string newUrl = AddNewSite(se.accessToken, se.hostUrl, se.eventName, se.eventDate);

 

//if it works, plug in the new site url

if (!string.IsNullOrEmpty(newUrl))

{

//create a new GraphObject item

YammerGraphObjectItem gi = CreateOpenGraphItem(newUrl, se.eventName, se.eventDate, se.twitterTags);

 

The CreateOpenGrahpItem method looks like this:

YammerGraphObjectItem gi = null;

 

YammerGraphObject go = new YammerGraphObject();

go.Activity.Action = "create";

go.Activity.Actor = new YammerActor("Steve Peschka", "speschka@yammo.onmicrosoft.com");

go.Activity.Message = "This is the discussion page for the " + eventName + " event on " + eventDate;

 

go.Activity.Users.Add(new YammerActor("Anne Wallace", "annew@yammo.onmicrosoft.com"));

go.Activity.Users.Add(new YammerActor("Garth Fort", "garthf@yammo.onmicrosoft.com"));

 

YammerGraphObjectInstance jo = new YammerGraphObjectInstance();

jo.Url = Url;

jo.Title = eventName; 

jo.Description = "This is the discussion page for the " + eventName + " event on " + eventDate; 

jo.Image = "https://socialevents.azurewebsites.net/images/eventplanning.png";

jo.Type = "document";

 

go.Activity.Object = jo;

 

string postData = go.ToString();

 

string response = MakePostRequest(postData, graphPostUrl, yammerAccessToken, "application/json");

 

//serialize the results into an object with the OG ID

if (!string.IsNullOrEmpty(response))

{

gi = JsonConvert.DeserializeObject<YammerGraphObjectItem>(response);

string newMsg = "Welcome to the Yammer discussion for the " + eventName +

" event, happening on " + eventDate + ".  We'll also be tracking external " +

"discussions about this event on Twitter by using the tags " + twitterTags + ".";

CreateOpenGraphPost(gi.object_id, newMsg);

}

One quick note about the code above.  You may notice that I made the OG object Type a “document”.  You can find the complete list of Types that are supported at http://developer.yammer.com/opengraph/#og-schema.  There isn’t a type for “web site” so I just used document, but you can obviously choose one that works for you.  Finally, assuming our OG was created successfully then we serialize the return JSON to get the YammerGraphObjectItem so we have the object_id we need to update it later on.  Then we create the first discussion post to the OG item in the CreateOpenGraphPost method.  It is extraordinarily simple, just the way I like it:

string msg = "body=" + Message + "&attached_objects[]=open_graph_object:" +

ID + "&skip_body_notifications=true";

 

//try adding the message

string response = MakePostRequest(msg, messageUrl + ".json", yammerAccessToken);

That’s it!  We’ve talked about the semantics of working with Yammer Open Graph items, and we’ve looked at the specific implementation we used in the CloudTopia application.  In the next post in this series we’ll look at adding a Web API component to your standard out of the box SharePoint App project.  This can be extremely simple, I regularly use them in my SharePoint Apps now so I hope you’ll tune in and read more about it.