Enhancing the Yammer Search Results and Messages Samples and Deserialization

Just as kind of strange coincidence, I was in working on some Yammer message JSON goo and realized that there were actually a couple of other important data elements that comes down in the JSON payload when you get a collection of messages from REST. That includes both querying for messages directly, as well as getting a collection back in search results. In addition to the messages themselves, there's also some info that can be used with the realtime API, as well as what it calls "references". The beauty of the references content is that it includes information about each user referenced in the collection of messages that are returned. I missed this the first time around, just one of the dangers at trying to stare at about 4k of JSON and hoping to extract everything useful out of it. Mucho love here to Fiddler and it's JSON parser for snapping me out of it. :-)

If you read the blog post on integrating search results here: https://blogs.technet.com/b/speschka/archive/2014/03/11/integrating-yammer-and-sharepoint-2013-search-results.aspx - you saw that when I got a list of search results back, I actually made a second call back to Yammer for each one to get information about the user associated with the message. By pulling in the references information I no longer need to make those additional round trips now.

So there are two things I wanted to briefly touch on here. First, I've updated the definition of the YammerMessages class; the JSON returned from Yammer is serialized into this class. I've now added additional properties into which the realtime and references information is hydrated when you call GetInstanceFromJSON. I've attached the updated class to this posting so you can download it and use it in your projects and then you will "automatically" get this new goodness. The second thing is I rewrote the section of code in the search WebAPI service I had so that it uses that References collection now instead of calling back to Yammer. I'm just going to paste in here the chunk of code from that solution that has changed; if you are going to build on that example I linked to above just plug in this new code:

foreach (YammerMessage ym in ysr.Messages.Messages)

{

                    #region used this method to get the user info by querying Yammer again

                   ////get the Yammer User that posted each message so we can pull in

                   ////their picture url

                   //string userUrl = oneUserUrl.Replace("[:id]", ym.SenderID);

                   //response = YammerREST.MakeGetRequest(userUrl, accessToken);

                   //YammerUser yu = YammerUser.GetInstanceFromJson(response);

                   #endregion

                   var yUser = from YammerMessagesReferences yr in ysr.Messages.References

                               where ym.SenderID == yr.ID

                               select yr;

                   YammerMessagesReferences ymr = yUser.First<YammerMessagesReferences>();

                   //add a new search results

                   //finds.Add(new SearchResult(yu.FullName, yu.WebUrl, yu.FirstName, ym.MessageContent.RichText, yu.PhotoUrl, DateTime.Parse(ym.CreatedAt), ym.WebUrl));

                    finds.Add(new SearchResult(ymr.FullName, ymr.WebUrl, ymr.UserName, ym.MessageContent.RichText, ymr.MugshotUrl, DateTime.Parse(ym.CreatedAt), ym.WebUrl));

                    iCount += 1;

                   if (iCount == 3)

                       break;

}

YammerMessages.cs.txt