How to Create Yammer Group Programmatically using C#

Sathish Nadarajan
 
Solution Architect
June 17, 2017
 
Rate this article
 
Views
5066

Nowadays, Yammer has become an integral part of Office 365 Sites. When we POST a data from our Site Collection, it gets stored in the Yammer.

When, we open the Context Menu of the File and Click on the POST, a Yammer Popup will be coming.

clip_image002

clip_image004

On the popup, we will be entering our text and click on POST. This will be reflected on the Yammer Site. There are many things, which we need to integrate with the Yammer. Let us focus them on the upcoming articles. But, Yammer and Office 365, has a tight coupling and there are lot more can be achieved, if we use them properly.

The Inserted comment can be visible on the Yammer site as below. But, if you could see, Yammer can be created with many groups. By default, “All Company” Group will be selected. And the Comment which we posted from the Office 365 will be posted in the Yammer Group “All Company”.

clip_image006

Now, let us see how to create a Yammer Group through the screen as well as C# Client side object model as well.

On the Yammer home page, click on the “Discover More groups” option.

clip_image008

On the right hand top corner, we can see the “Create Group” Button.

clip_image010

On the Screen, enter the group name, members, whether it is a Public or Private group.

clip_image012

Click on “Create Group”

That’s it. Yammer Group would have been created.

.

clip_image014

With this introduction, let us see, how to create a Yammer Group Programmatically.

It is a straight forward method

 using Microsoft.SharePoint.Client;
 using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
 using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
 using System.IO;
 using System.Net;
 using System.Text;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             CreateYammerGroup();
         }
 
         public static void CreateYammerGroup()
         {
             string url = "https://www.yammer.com/api/v1/groups.json";
 
             string access = "&private=false"; // This is for PUBLIC group.  For Private Group - "&private=true";
             string groupUrl = string.Format("{0}?name=" + "YammerGroupName" + "{1}", url, access);
 
             string result = PostYammerJson(groupUrl, "<<ADMIN ACCESS TOKEN OF THE YAMMER TENANT>>");
         }
 
         public static string PostYammerJson(string url, string accessToken)
         {
             //make the request
             string json = null;
             HttpWebRequest YammerRequest = HttpWebRequest.Create(url) as HttpWebRequest;
             YammerRequest.Method = "POST";
             YammerRequest.ContentType = "application/json; odata=verbose";
             YammerRequest.Accept = "application/json; odata=verbose";
             YammerRequest.ContentLength = 0;
             YammerRequest.Headers.Add("Authorization", string.Concat("Bearer ", accessToken));
 
             using (HttpWebResponse response = YammerRequest.GetResponse() as HttpWebResponse)
             {
                 Encoding encode = Encoding.GetEncoding("utf-8");
                 StreamReader reader = new StreamReader(response.GetResponseStream(), encode);
                 json = reader.ReadToEnd();
             }
             return json;
         }
 
     }
 }
 

Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment