Sunday, August 17, 2014


In last post I discussed about how to write code for REST API calls. In this post we will learn how to fetch all public messages of current logged in user. This article will also explains how to implement pagination – get more older messages.  There are various endpoints for messages to get various types of messages. We will see how to get all public messages  of user in this post.

To get all public messages following endpoint is useful:

HTTPS://api.yammer.com/messages.json

Or you can use only 'messages.json' if you use yam.platform.request for call.

Parameters:

1. Limit: Number of messages in response.
2. newer_than: With this parameter endpoint returns all new messages than given messages.
3. older_than: With this parameter endpoint returns older messages than given message. 

These parameters are used in below example.In both parameters you have to pass the message id.

Explanation of API response:

When API request gets executed successfully it returns the response otherwise returns error message. In successful response it returns number of messages in array. It also returns one more array: references; see below image.




Let’s see each array details one by one.

1.      Array - messages :

This array contains details of each message like: body, attachments, created date time, message id, sender id, web URL, network id, liked by, etc. With that much information we can create Yammer Newsfeed.



2.      Array – references:

This array contains additional information. This is supportive information for the actual messages. For example it contains user information which has published message which comes in messages array. In messages array we can get only id sender and for detail information like user full name, profile picture we can refer references array.




Complete Script:

<script type="text/javascript" data-app-id="00WSJ73Q4pSjCjYE64f3A" src="https://assets.yammer.com/assets/platform_js_sdk.js"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

var older_than= "";
var limit = 5;
var htmlstr = "<div style='margin-bottom:15px;' class ='row' id='[ID]'><div class ='col-md-4'><a href='[USERWEBURL]' target='_blank'><img src='[IMG]' class='img-responsive'/><br/>[SENDERNAME]</a></div><div class ='col-md-8'><p>[TEXT]<br/>[DATE]</p></div></div>";

     function GetAllMessages()
{
            yam.getLoginStatus(function (response) {
                if (response.authResponse) {

if(older_than == "")
{
                    yam.platform.request(
                      {
                          url: "messages.json"
                      , method: "GET"
                      , data: { 'limit': limit}
                      , success: function (responseData) {
                          BindHtml(responseData,false);
                      }
                      , error: function (msg) { alert("Error..." + msg); }
                      }
                    )
}
else
{
yam.platform.request(
                      {
                          url: "messages.json"
                      , method: "GET"
                      , data: { 'limit': limit, 'older_than': older_than}
                      , success: function (responseData) {
                          BindHtml(responseData,true);
                      }
                      , error: function (msg) { alert("Error..." + msg); }
                      }
                    )
}

                } else {
if(older_than == "")
{
                    yam.platform.request(
                      {
                          url: "messages.json"
                      , method: "GET"
                      , data: { 'limit': limit}
                      , success: function (responseData) {
                          BindHtml(responseData,false);
                      }
                      , error: function (msg) { alert("Error..." + msg); }
                      }
                    )
}
else
{
yam.platform.request(
                      {
                          url: "messages.json"
                      , method: "GET"
                      , data: { 'limit': limit, 'older_than': older_than}
                      , success: function (responseData) {
                          BindHtml(responseData, true);
                      }
                      , error: function (msg) { alert("Error..." + msg); }
                      }
                    )
}
                }
            });

}

function BindHtml(responseData,isNext)
{
var finalhtml = "";
var last;
for (var j = 0; j < responseData.messages.length; j++) {
var sender = $.grep(responseData.references, function(e){ return e.id == responseData.messages[j].sender_id; });
//var html = htmlstr.replace("[TEXT]",responseData.messages[j].content_excerpt);
var html = htmlstr.replace("[TEXT]",responseData.messages[j].body.rich);
html = html.replace("[DATE]",responseData.messages[j].created_at);
html = html.replace("[ID]",responseData.messages[j].id);
html = html.replace("[IMG]",sender[0].mugshot_url); 
html = html.replace("[USERWEBURL]",sender[0].web_url);
html = html.replace("[SENDERNAME]",sender[0].full_name);

finalhtml = finalhtml + html;
last = responseData.messages[j].id;
}
if(!isNext)
{
$("#messagesdiv").html(finalhtml);
}
else{
$("#" + older_than).after(finalhtml);
}
older_than = last;
}

GetAllMessages();

</script>

Explanation of more Feeds script:

To understand how the Yammer REST API works refer my previous post. In this script GetAllMessages() method gets called at first time and as older_than is empty then the if block gets executed. With successful execution BindHtml() gets called and it generates the final html and binds to div element. Now the id of last message in the array is the value of older_than variable.
Now when More button gets clicked GetAllMessages() method gets called. But now older_than variable has value so ‘else’ block gets executed and in BindHtml() method html gets created and appended to last message.

In this way we can create Yammer Newsfeed to display public messages of user.  It is very simple to implement too.

Happy SharePoint coding.

0 comments:

Post a Comment