Sunday, December 21, 2014


We will continue our exploration in Yammer world to learn new things. Till now  we have seen how to get messages for logged in user, how to post message in Yammer. In this article we will specifically see how to fetch messages of a specific group.

We have seen in my previous posts where you can use Yammer web part and embed code to show messages of a particular group. But the problem with them is they are not mobile device complaint; they are not RWD complaint. So what if you are planning to have your SharePoint site mobile devices complaint or RWD complaint? How can you show the group messages on those devices?

The option you left with is Yammer REST APIs. But if you check the Yammer API documentation site then you will not find any REST service to get the group messages. With many combination and try I got following REST URL can help you to get the messages. This Url is not mentioned in Yammer documentation but it is working; hopefully we will get better documentation in future.

URL: 'messages/in_group/[Group ID].json'

You need to provide your intended group id in above URL.

Following is code snippet which shows how to fetch messages of a specific group.
Code Snippet:

<script type="text/javascript" data-app-id="[data-app-id]" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script> 
<script type="text/javascript">
var resultslimit = 20;
var htmltemplatestr = "<div ><div ><a href='[USERURL]' target='_blank'><img src='[IMG]' /><br/>[SENDER]</a></div><div ><div ><p>[TEXT]<br/>[DATE]</p></div></div></div>";
yam.getLoginStatus(
function (response) {
if (response.authResponse) {
GetMsg();
}
else {
yam.platform.login(function (response) {
if (!response.authResponse) {
GetMsg();
}
});
}
});
function GetMsg(){
var groupID = "4409501";
yam.platform.request(
 {
 url: "messages/in_group/" + groupID + ".json"
 , method: "GET"
 , data: { 'limit': resultslimit}
 , success: function (responseData) {
 ShowHtml(responseData,false);
 }
 , error: function (msg) { 
 alert("Error..." + msg); 
 }
 }
)
}
function ShowHtml(responseData,isNext)
{
var htmlTobind = "";
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 = htmltemplatestr .replace("[DATE]",DateStr(responseData.messages[j].created_at));
//this returns the all parents messages in groups
if(responseData.messages[j].replied_to_id == null)
{
html = html.replace("[TEXT]",responseData.messages[j].body.rich);
}
html = html.replace("[IMG]",sender[0].mugshot_url); 
html = html.replace("[USERURL]",sender[0].web_url);
html = html.replace("[SENDER]",sender[0].full_name);
htmlTobind = htmlTobind + html;
last = responseData.messages[j].id;
}
$("#msgsection").html(htmlTobind);
}
function DateStr(dateStr) {
    var monthNames = new Array("January", "February", "March",
        "April", "May", "June", "July", "August", "September",
        "October", "November", "December");
    var dt = new Date(dateStr);
    var curr_date = dt.getDate();
    var curr_month = dt.getMonth();
    var curr_year = dt.getFullYear();
    return monthNames[curr_month] + " " + curr_date + " at " + dt.getHours() + ":" + dt.getMinutes();
</script>  

<div style="margin-top:20px;height:800px;overflow-y:scroll;">
<div id="msgsection" style="width:100%;">
</div>
</div>



Happy Yamming and SharePoint coding.

0 comments:

Post a Comment