Wednesday, August 20, 2014

In post I discussed about how to use REST API calls. As I said APIs are very helpful to implement special requirements. Displaying a user feed can be a special case.

As a CEO of company you always wants to send your messages to all of the employees and wants all of them received the message quickly. To do this you post your messages on the Yammer. Many of the people don’t want to switch between the web sites of your intranet and yammer site. So you want that company’s intranet portal should show my messages on the home page.

Now think as developer CEO contacts to you with same requirement that he wants to show his messages on the home page of intranet portal. To achieve this you can use the below REST API endpoint.

To get user messages following endpoint is useful:

HTTPS://api.yammer.com/messages/from_user/[User Id].json

Or you can use only ‘messages/from_user/[User Id].json’ if you use yam.platform.request for call.

Script to fetch the User Messages:

yam.platform.request(
                      {
                          url: "messages/from_user/[User Id].json "
                      , method: "GET"
                      , data: { 'limit': limit}
                      , success: function (responseData) {
                          BindHtml(responseData,false);
                      }
                      , error: function (msg) { alert("Error..." + msg); }
                      }
                    )

You can use the script of the Post: http://www.sharepointyammertech.com/2014/08/yammer-sharepoint-how-to-implement.html . Copy the complete script and just replace the REST endpoint/URL with the ‘messages/from_user/[User Id].json’. And use the script you can get the user posted messages.


Happy SharePoint coding.

In post I discussed about how to use REST API calls and in post – I explained how to get all public messages using REST API call. The post explains how to use Message REST endpoint and get the response; how to use that response and show the results on the page in the form of html.

In this post we will see how to get all Top conversations user has. These are the conversations which are most relevant to the user. Getting most relevant and useful conversation on top or separately is very common requirement in the enterprise social network. Most of the expertise are interested in topics which are relevant to their work, their daily activity. So why do Yammer remain behind? They have provided option in Yammer site to see the Top conversation. But what about portal where Yammer has been integrated? How you can show the Top messages to the end users when they log in to portal?

So the answer is Yammer REST API. Yammer has provided below endpoint using which you can fetch the Top messages and display on the page.

To get Top Conversation following endpoint is useful:

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

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

Script to fetch the Top conversation:

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

You can use the script of the Post: http://www.sharepointyammertech.com/2014/08/yammer-sharepoint-how-to-implement.html . Copy the complete script and just replace the REST endpoint/URL with the 'messages/algo.json'. And use the script you can get the all Top conversations.

Happy SharePoint coding.

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.