Tuesday, October 27, 2015

Programmatically post a message to Yammer from SharePoint Online

In my previous post, I showed how to programmatically embed a Yammer feed into a SharePoint Online page, using the JavaScript SDK for Yammer and the Yammer REST API. In this post I will show how to post a message into a Yammer group from a SharePoint Online page, using again the JavaScript SDK for Yammer and the Yammer REST API.



1 - Adding a new feature to post a message to a Yammer group, from SharePoint on line


In my previous post (the one for displaying the Yammer feed into SharePoint Online) I have explained why you have to create a Yammer App and how to do it. I also explained the basis of programming for Yammer with the Yammer Javascript SDK and the REST API. I advice you to do the previous tutorial.

Here, I will just explain some pieces of the code. You will find the complete page on the dedicated GitHub repositery.

The first function gets the Yammer topic value (selected by the user with the radio button). It is the place to manage authentication if needed. I have managed authentication for the getMyFeed function. It is explained also in the previous post.

function postMessage() {
   topicToPost=getTopicValue();
   yamPostRequest(this);
   /*If needed you have to manage authentication as in the script "authentication" I used for getting the feed (line 130)*/
   }

then you send the post to Yammer after having checked that no elements are missing. Because you cannot send a topic with an empty value you have to check if there is a topic:
if (topicToPost.length>0){
     yam.platform.request(
     { 
       url: "https://api.yammer.com/api/v1/messages.json"
       , method: "POST"
       , data: {
         "body" :  msg_value
        ,"group_id" : groupID
        ,topic1 : topicToPost
        ,"cc": "[[user:1557787745]],[[user:1557794361]]"//use your own user ID to notify them
        //,direct_to_id : "1537569057" seems not to work
        ,"skip_body_notifications" : "true"
      }
       , success: function (msg) { 
       document.getElementById('msg_body').value="";
      //alert("Post was Successful!"); 
      }
       , error: function (msg) { //alert("Your message cannot be published\nThe request to Yammer didn't work... "); 
      }
     })
  }else {
      //if there is no topic you do the same request except you avoid the "topic1" parameter
  }

4 comments:

Unknown said...

I want to post on yammer page not group using rest api or any java script.
We have feed type as opengraph and object is page.
Now to auto post on this page on any button click.any help in this regard will b much appreciated

Marc Charmois said...

Hi Mayank,

thank you for your interest in my post. I will be happy to help you, but I am afraid not to understand what you want to do exactly. Would you give me more detailed explanations please?
Thanks.

Marc

Unknown said...

Hi I have been trying to do the same but am unable to post if the file is a pdf or beyond a certain size. Any help wpuld be appreciated.

Anonymous said...

Hi,

I tried the code suggested but getting error message as:
{"Readystate:0."status":0,statustext:error}