Thursday, December 11, 2014


We have seen how to fetch messages from Yammer for various purposes using Yammer REST API. Now is the time to post messages to Yammer. We will see how to create a component which can be used to post messages to yammer.

REST URL used to post messages is: "messages.json"

Once message gets posted successfully it returns current message in response so user can refresh your component’s screen. You can post only one message at a time using this Yammer REST.

Following is the complete code to post message:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
 <script type="text/javascript" data-app-id="[Data-App-ID]" src="https://assets.yammer.com/assets/platform_js_sdk.js"></script>
<script type="text/javascript">
function post() {                             
                var messageToPost = document.getElementById("txtMessage").value; 
yam.getLoginStatus( function(response) {
if (response.authResponse) {
yam.platform.request(
 { url: "messages.json"
 , method: "POST"
 , data: { "body": messageToPost, "group_id": "4409501" }
 , success: function (msg) { alert("{Post was Successful!}: " + msg); }
 , error: function (msg) { 
 alert("Post was Unsuccessful..." + msg); 
 }
 }
)
} else {
yam.platform.login( function (response) {
 if (!response.authResponse) {
yam.platform.request(
 { url: "messages.json"
 , method: "POST"
 , data: { body: messageToPost, group_id: 4409501 }
 , success: function (msg) { alert("{Post was Successful!}: " + msg); }
 , error: function (msg) { 
 alert("Post was Unsuccessful..." + msg); 
 }
 }
);
 }
});
}
});
}
</script>
    <div>
        <b>Share an Update.</b>
    </div>
    <div style="width:500px;">
        <textarea id="txtMessage" style="height:100px;width:100%;"></textarea>
    </div>
    <div>&nbsp;</div>
    <div>
    <button onclick='post()'>Post on Yammer!</button>
    </div>

In above code you need to provide your Data-App-ID.  In this way you can post message to any Yammer group. Don't forget to provide your group id. 


Happy SharePoint 2013.

2 comments:

  1. I have tried your code and filled in the app ID (client ID) and group ID. I am getting an error shown below- any recommendations? Thanks in advance.

    XMLHttpRequest cannot load https://www.yammer.com/platform/login_status.json?client_id=#######. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://......com' is therefore not allowed access.

    ReplyDelete
  2. Hi Allison Wiener,

    You need to use following yammer js library, yammer has changed it recently.

    https://c64.assets-yammer.com/assets/platform_js_sdk.js

    Also follow this url:
    https://developer.yammer.com/v1.0/docs/js-sdk .

    Thank you.

    Regards,
    Rahul

    ReplyDelete