Iterating through a SharePoint discussion list

One of those things that you think should be pretty straightforward…

Credit to Dave Ginn for finding the solution.

List itemList = new List(); foreach (SPListItem discussion in discList.Folders) { //discussion.Title is discussion title SPQuery query = new SPQuery(); query.Folder = discussion.Folder; SPListItemCollection messages = discList.GetItems(query); itemList.Add(new Discussion(discussion.Title, messages.Count)); } //sort by most comments itemList.Sort(delegate(Discussion p1, Discussion p2) { return p2.Comments.CompareTo(p1.Comments); });

Here’s the site I used for reference: