Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Twitter lists in .NET

DZone's Guide to

Twitter lists in .NET

·
Free Resource

If you have used Twitter, you probably know that not long ago a new feature was added to the service – lists. Twitter users are able to sort their followers/following in lists for easier access to the information those are providing, now sorted.

In this article I am talking about a possible implementation of an application that leverages the power of Twitter lists in a .NET manner. Before we go further, I must say that to use the Twitter API, the developer must be familiar with XML (or JSON) specifications as well as with the way it can be accessed from .NET. Also, I would highly recommend getting yourself used to working with HTTP requests, as at this point this is the foundation of the API.

To start, let’s take a look under the hood at lists.

A twitter user can create a list. It can be for any groups of Twitter users – friends, fellow coworkers or just random people. Once created, the list founder should add users to that list. Once done, other people can follow a list and get updates from specific groups of people. A single user can create multiple lists, be a member of multiple lists, as well as follow multiple lists.


I am going to start with the .NET implementation of the list creation method.

private void CreateList(string name, string username, string password, string mode = "", string description = "")
{
StringBuilder URL = new StringBuilder();
URL.Append("http://api.twitter.com/1/user/lists.xml?name=" + name);

if (!string.IsNullOrWhiteSpace(mode))
{
URL.Append("&mode=" + mode);
}

if (!string.IsNullOrWhiteSpace(description))
{
URL.Append("&description=" + description);
}

HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create(URL.ToString());
messageRequest.Method = "POST";
messageRequest.Credentials = new NetworkCredential(username, password);
messageRequest.ContentLength = 0;
messageRequest.ContentType = "application/x-www-form-urlencoded";
WebResponse response = messageRequest.GetResponse();
StreamReader sReader = new StreamReader(response.GetResponseStream());
string responseStr = sReader.ReadToEnd();

Debug.Print(responseStr);
}


It is pretty simple. Mode and Description are optional. The mode can be either Private or Public (setting the access level for the list). Once the developer passes the needed parameters, the URL is built (depending on the parameter presence) and passed to the web server as a HTTP request. The response is printed in the Output window in Visual Studio, but you can modify this to return a string value. Also, as you see, I am using XML for this example. If you are more familiar with JSON, just replace the XML extension with JSON.

Now that you created a list, there is a way to update it through the Twitter API.

private void UpdateList(string name, string username, string password, string newName = "", string mode = "", string description = "")
{
StringBuilder URL = new StringBuilder();
URL.Append("http://api.twitter.com/1/"+ username + "/lists/" + name + ".xml");

if (!string.IsNullOrWhiteSpace(newName))

{
URL.Append("?name=" + newName);
}

if (!string.IsNullOrWhiteSpace(mode))
{
if (!URL.ToString().EndsWith(".xml"))
URL.Append("&mode=" + mode);
else
URL.Append("?mode=" + mode);
}

if (!string.IsNullOrWhiteSpace(description))
{
if (!URL.ToString().EndsWith(".xml"))
URL.Append("&description=" + description);
else
URL.Append("?description=" + description);
}

HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create(URL.ToString());
messageRequest.Method = "POST";
messageRequest.Credentials = new NetworkCredential(username, password);
messageRequest.ContentLength = 0;
messageRequest.ContentType = "application/x-www-form-urlencoded";
WebResponse response = messageRequest.GetResponse();
StreamReader sReader = new StreamReader(response.GetResponseStream());
string responseStr = sReader.ReadToEnd();

Debug.Print(responseStr);
}

As you see, this method is very similar to the previous one, although the request URL is different and there is a newName parameter. The parameters that were used in the previous snippet to create a list are now optional – those are only used to update the existing list information.

What if I want to delete a list? This is done this way:

private void DeleteList(string name, string username, string password)
{
string URL = "http://api.twitter.com/1/" + username + "/lists/" + name + ".xml";
WebRequest request = HttpWebRequest.Create(URL);
request.Method = "DELETE";
request.ContentLength = 0;
request.Credentials = new NetworkCredential(username, password);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());

string responseString = responseStream.ReadToEnd();

Debug.Print(responseString);
}

All parameters are required here, however, the entire function is much shorter – there is no additional information needed to delete a list but the list ID. Also, if this function is implemented in an application, I would recommend throwing a warning before the actual DELETE request – the list will be deleted instantly and there is no way to restore it.

A Twitter list without users in it is meaningless. Now, to actually add a Twitter user to the list, the following function can be used:

private void AddToList(string username, string password, string list, string member)
{
string URL = "http://api.twitter.com/1/" + username + "/" + list + "/members.xml?id=" + member;

HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create(URL);
messageRequest.Method = "POST";
messageRequest.Credentials = new NetworkCredential(username, password);
messageRequest.ContentLength = 0;
messageRequest.ContentType = "application/x-www-form-urlencoded";
WebResponse response = messageRequest.GetResponse();
StreamReader sReader = new StreamReader(response.GetResponseStream());
string responseStr = sReader.ReadToEnd();
Debug.Print(responseStr);
}

Mind that all parameters here are required as well.

To delete a user from a list:

private void DeleteFromList(string username, string password, string list, string member)
{
string URL = "http://api.twitter.com/1/" + username + "/" + list + "/members.xml?id=" + member;
HttpWebRequest messageRequest = (HttpWebRequest)WebRequest.Create(URL);
messageRequest.Method = "DELETE";
messageRequest.Credentials = new NetworkCredential(username, password);
messageRequest.ContentLength = 0;
messageRequest.ContentType = "application/x-www-form-urlencoded";
WebResponse response = messageRequest.GetResponse();
StreamReader sReader = new StreamReader(response.GetResponseStream());
string responseStr = sReader.ReadToEnd();
Debug.Print(responseStr);
}


This almost identical to the function that is used to add a user, however it is sending a DELETE request. As you see, manipulating Twitter lists isn’t that complicated after all. An important thing to know is that I only scratched the surface in this article. For a complete list of methods, I recommend you reading the official Twitter API documentation.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}