Wednesday, February 4, 2015


Many of my friends have asked me about how to get groups to which user has joined. We don't have such RESTAPI URL available in Yammer documentation. But sometimes this is very important to get users all groups. Then how can you get this information? So my friend here we go!

Though API is not there but you can use other REST API URL using which you can get the groups of a user. And that API URL is nothing but Autocomplete '. The main use of this API url is to give suggestions to user when types group name or user name. It returns few records on the basis of match found. This API has its own parameters but we will see them in detail in another article. In this article we will focus on to get the user's groups.

Then main parameters which are required here are: Prefix and models.

URL: autocomplete/ranked .

Prefix: In our case we will provide the email address or short name of user as prefix.

Models: We will provide 'user' and count '1'. Which means match for user type and return one record for provided email address in prefix.

Another REST url you needed is to get the details of group on the basis of id returned by above REST url. Details of second url:

URL: /groups/[Group Id].json 

Parameter: you need to pass the group id in above url.

By this input you will get the detail of that particular user. And in that detail you can get the group list as shown in below pic.



Following is the complete script which returns groups of a user. You need to provide data app id and user email address in order to execute this code.

Complete Code:


<script type="text/javascript" data-app-id="[YourAppId]" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script> 
<script type="text/javascript">
yam.getLoginStatus(
function (response) {
if (response.authResponse) {
}
else {
yam.platform.login(function (response) {
if (!response.authResponse) {
}
});
}
});

function GetMsg(){
var prefixValue = "[USER_SHORT_NAME]";
yam.platform.request({
url: "autocomplete/ranked",
method: "GET",
accept: "application/json",
data: { prefix: prefixValue, models: 'user:1,group:0'},
success: function (responseData) {
for(var i=0; i< responseData.user[0].group_ids.length; i++)
{
yam.platform.request(
 { url: "groups/" + responseData.user[0].group_ids[i] + ".json"
 , method: "GET"
 , success: function (msg) {  
var node = document.createElement("LI");
var textnode = document.createTextNode(msg.full_name);
node.appendChild(textnode);
document.getElementById("myGroups").appendChild(node);  
 }
 , error: function (msg) { 
 
 }
 }
);
}
},
error: function (msg) {
alert("Error..." + msg);
}
}
)
}

</script> 
<input type="button" onclick='GetMsg()' value="Click Me"></input> 
<div id="groupsId">
<ul id="myGroups">
</ul>

</div>

Output Screen:




Happy Yammer coding!

0 comments:

Post a Comment