I have SharePoint 2007 discussion list. I want to iterate over all items in the list, so I do the following:

var list = web.Lists["discussion"];
var listItems = list.Items;
foreach (var listItem in ListItems)
{
    // do something
}

However the iteration doesn't seem to work. The Items collection is always empty. Interesting to notice

list.ItemCount

will return "4", whereas

list.Items.Count

will return "0".

Has anyone encounterd such a problem before? I tried another list (custom list), and there I can iterate just fine. Seems to be specific to the discussion-list.

link|improve this question

38% accept rate
feedback

2 Answers

up vote 3 down vote accepted

Looks like the trick is to use list.Folders:

foreach (SPListItem folder in myDiscussionList.Folders)
{
    // do something
}

Source: How to Read All Discussions and Replies

link|improve this answer
3  
+1, but I'd like to add: the point here is that SPList.ItemCount returns number of items including folders, meanwhile SPListItemCollection.Count does not include folder count. The source (Remarks section): msdn.microsoft.com/en-us/library/… – Andrey Markeev Sep 26 '11 at 17:30
That link you provided is ... well I don't like it. btw @omlin that +1 is mine, so where is yours :) – Vedran Rasol Sep 26 '11 at 20:25
Oops... I confess, but actually, I'm not a greedy person, just immensely exhausted and almost sleeping.. :( Rough day at work. Fixed. – Andrey Markeev Sep 26 '11 at 20:33
That worked! How should one know, that discussions are folders and not items? Who would have thought of that?! – nyn3x Oct 4 '11 at 15:00
feedback

Here is my answer to somewhat similar question. It is 2010 solution in Powershell but you can easily convert it Moving a Discussion from a Discussion Board to another?

Addition:

I posted my original answer from my mobile device so I feel obligated to provide more info.

To simplify things: In discussion list discussions are folders and messages are items inside folders. Since there are no items on 'root' list.Items.Count will return 0 and list.ItemCount will return number of folders/discussions.

And now the fun stuff.

Get all discussions (@KitMenke already provided this)

foreach (SPListItem discussion in list.Folders)
{

}

Get all discussions with all messages

foreach (SPListItem discussion in list.Folders)
{
    //discussion.Title is discussion title
    SPQuery query = new SPQuery();
    query.Folder = discussion.Folder;
    SPListItemCollection messages = list.GetItems(query);
    foreach(SPListItem message in messages)
    {
        // message["TrimmedBody"] is message body
    }
}

Get all messages

SPQuery query = new SPQuery();
query.ViewAttributes = "Scope='Recursive'";
SPListItemCollection messages = list.GetItems(query);
foreach (SPListItem message3 in messages3)
{
    // message.DisplayName is discussion title !
    // message["TrimmedBody"] is message body
}

Get all messages from current user

SPQuery query = new SPQuery();
query.Query = "<Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID/></Value></Eq>";
query.ViewAttributes = "Scope='Recursive'";
SPListItemCollection messages = list.GetItems(query);
foreach (SPListItem message in messages)
{
    // message.DisplayName is discussion title !
    // message["TrimmedBody"] is message body
}
link|improve this answer
+1 for the "Addition" part. It could be fairly useful to have these ready-to-use code snippets in sight when working with Discussion Boards. – Andrey Markeev Sep 26 '11 at 20:44
+1 very nice. You have both powershell and object model covered! I removed the 2007 tag from the question because it should be the same. – Kit Menke Sep 26 '11 at 21:19
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.