Skip to content

Instantly share code, notes, and snippets.

@brianly
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianly/ffac21a49495fabf371e to your computer and use it in GitHub Desktop.
Save brianly/ffac21a49495fabf371e to your computer and use it in GitHub Desktop.
Update a user profile in Yammer with a PUT request. Could be improved by using the newer HttpClient library.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const int userId = 5189753;
const string accessToken = "";
// N.B. Use something other than a dictionary to support passing
// education[] multiple on the query string. This will error
// with duplicate key warning.
var propertiesToUpdate = new Dictionary<string, string>
{
{"full_name", "This is a new full_name"},
{"job_title", "This is a new job_title"},
{"location", "This is a new location"},
{"mobile_telephone", "This is a new mobile_telephone"},
{"work_extension", "This is a new work_extension"},
{"significant_other", "This is a new significant_other"},
{"interests", "This is a new interests value"},
{"summary", "This is a new summary value"},
{"expertise", "This is a new expertise value"},
{"work_telephone", "0118 999 881 999 119 7253"},
{"department_name", "New dept. name"},
{"education[]", "UCLA,BS,Economics,1998,2002"}, // undergraduate only
{"previous_companies[]", "Geni.com,Engineer,Building desktop apps,2005,2008"}, // first only
};
UpdateUser(userId, accessToken, propertiesToUpdate);
}
private static void UpdateUser(int userId, string accessToken, Dictionary<string, string> newProperties)
{
var userUrl = CreateUserApiUrl(userId);
var requestParameters = CreateRequestParameters(newProperties);
// PUT https://www.yammer.com/api/v1/users/9123456.json?work_telephone=123456&department_name=deptname HTTP/1.1
var putRequestUrl = string.Format("{0}?{1}", userUrl, requestParameters);
var putRequest = (HttpWebRequest)HttpWebRequest.Create(putRequestUrl);
putRequest.Method = "PUT";
putRequest.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
string result;
var putResponse = (HttpWebResponse)putRequest.GetResponse();
using (var rdr = new StreamReader(putResponse.GetResponseStream()))
{
result = rdr.ReadToEnd();
}
}
private static string CreateRequestParameters(Dictionary<string, string> newProperties)
{
var body = "";
foreach (KeyValuePair<string, string> item in newProperties)
{
body += string.Format("{0}={1}&", item.Key, item.Value);
}
return body;
}
private static Uri CreateUserApiUrl(int userId)
{
return new Uri(string.Format("https://www.yammer.com/api/v1/users/{0}.json", userId));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment