DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Twitter lists in .NET

Denzel D. user avatar by
Denzel D.
·
Apr. 29, 10 · Interview
Like (0)
Save
Tweet
Share
5.20K Views

Join the DZone community and get the full member experience.

Join For Free

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.

twitter

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • NoSQL vs SQL: What, Where, and How
  • Java REST API Frameworks
  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • 5 Best Python Testing Frameworks

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: