How we can retrieve Discussion Topics (items) from a Discussion List ?

Whenever we try to access the items (discussion topics) in a discussion list it will return 0 , however olist.ItemCount will show the exact number (in the screen shot - there are 2 items in that discussion list) of discussion topics in your discussion list.

Why ?

clip_image002

The reason is – In discussion list, all discussion topic items are folders. Thus we have to access those items using Folders collection. See the below sample code to find out the items (discussion topics) in a discussion list. Also we need to remember the following while trying to check the item count in some conditions.

    1: oList.Items.Count; // this will give the count of items – in discussion list only replies
    2:  
    3: oList.ItemCount;// this will return the count of folders and items 
    4:  
    5: using System;
    6: using System.Collections.Generic;
    7: using System.Text;
    8: using Microsoft.SharePoint; 
    9:  
   10: namespace Test
   11: {
   12:  
   13:     class Program
   14:     {
   15:  
   16:         static void Main(string[] args)
   17:         {
   18:             SPSite site = new SPSite("https://blr3r07-19c:5794/sites/test");
   19:             SPWeb web = site.OpenWeb();
   20:             SPList oList = web.Lists["SharePoint Discussion"];
   21:             int iItemCount = oList.Items.Count; // this will give the count of items – in discussion list only replies
   22:             int iTotalCount = oList.ItemCount;// this will return the count of folders and items 
   23:  
   24:             if (oList.Folders.Count > 0)
   25:             {
   26:  
   27:                 Console.WriteLine(oList.Folders[1].Url);              
   28:  
   29:             } 
   30:  
   31:             Console.ReadLine();          
   32:  
   33:         }     
   34:  
   35:     }
   36:  
   37: }