Yammer from C# – create a group

There’s a surprisingly woeful lack of doco and information about using the API for Yammer – from C# especially – there’s more for the JavaScript way, but not for .NET.

The one I need – to create a Yammer Group – is this one :

https://www.yammer.com/api/v1/groups.json?name={0}&private={1}

The best approach is to simply call the REST API – using the .NET Web Client classes (HttpWebRequest).

It follows that you need to do a POST to this URL – the clincher is to define an ‘accessToken’ to do so.   This is like a SharePoint 2013 style ClientId/ClientSecret.

Click here to register your app, and get the token – it is then registered against YOUR Yammer network.   

image

You’ll get a CLIENTID and CLIENTSECRET – need to plug these into the following URL >

https://www.yammer.com/oauth2/authorize?client_id=[:client_id]&redirect_uri=[:redirect_uri]

And then you’ll get a CODE value.    THEN, need to call the following >

https://www.yammer.com/oauth2/access_token.json?client_id=[:client_id]&client_secret=[:client_secret]&code=[:code]

And you’ll get a TOKEN – which is the magic bean you need for the following C# code.

Here’s some doco about it >

Then – you can just call the following method within C# :

private static string PostYammerJson(string url, string accessToken)
{
    //make the request
    string json = null;
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = “POST”;
    request.Headers.Add(“Authorization”, “Bearer” + ” ” + accessToken);
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        Encoding encode = Encoding.GetEncoding(“utf-8”);
        StreamReader reader = new StreamReader(response.GetResponseStream(), encode);
        json = reader.ReadToEnd();
    }
    return json;
}

Within the C# code – I can then do this :

string postUrl = “https://www.yammer.com/api/v1/groups.json?name=CoolPeople&private=false”;

//replace with what you get from Yammer register App page

string accessToken = “1234567890qwertyuiop”;     

PostYammerJson(postUrl, accessToken);

This is a GREAT pattern – thanks to the folk at the Microsoft PnP on GitHub.

There’s a whole solution/project that does this – and a LOT more – great examples :

https://github.com/OfficeDev/PnP/tree/master/Scenarios/Provisioning.Yammer

🙂

Advertisements

One thought on “Yammer from C# – create a group

  1. Pingback: Create Yammer group as another user | Cloud Developer Space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s