none
Get Number of Replies in a Discussion Board RRS feed

  • Question

  • Hi Everyone,

    I'm trying to get the number of replies for each thread in my discussion board...I've successfully able to achieve it using:

    foreach (SPListItem folder in list.Folders)
    
    {
    
    foreach (SPListItem item in list.Items)
    
    {
    
    if (item.DisplayName == folder.DisplayName){
    
    count++;
    
    }
    
    }
    
    }
    
    
    The problem is that my dicussion board has so many items that the inner loop takes too long to parse...My next idea was to do an SPQuery and get the SPListItemCollection...and grab the count from that, but I'm having issues with the CAML query keeps throwing an exception...any ideas? I was thinking of something like this:
    foreach(SPListItem folder in list.Folders)
    
    {
    
    string query = "<Where><Eq><FieldRef Name='Path' /><Value Type='Text'>subsiteName/Lists/"+folder.Title+"</Value></Eq></Where>";
    
    SPQuery Oquery = new SPQuery();
    
    Oquery.Query = query;
    
    SPListItemCollection coll = list.GetItems(Oquery);
    
    count = coll.Count;
    
    }
    
    

    Thanks!
    Friday, September 11, 2009 7:41 PM

Answers

  • Hi,

    You should use the Folder property of the SPQuery class (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.folder.aspx) to restrict to restrict the CAML query to the folder.

    foreach (SPListItem folderItem in list.Folders)
    {
      SPQuery query = new SPQuery();
      query.Folder = folderItem.Folder;
      SPListItemCollection coll = list.GetItems(query);
      count = coll.Count;
    }

    Peter
    • Marked as answer by Naheed_A Monday, September 14, 2009 4:17 PM
    Friday, September 11, 2009 7:52 PM
  • Howdy,

    Here you go:

    private void GetDiscussionItemReplyCounts()
    {
         SPWeb web = new SPSite("http://siteurl").OpenWeb();
    
         using (web)
         {
             SPList disList = web.GetList(web.Url + "/Lists/Duscussion Board");
    
             foreach (SPListItem item in disList.Items)
             {
                 //item - is a discussion in your list
    
                 //Replies count for current item
                 int replyCount = item.ListItems.Count;
             }
    
         }
    }

    Please let me know, If you need more help.

    Thanks,


    Gopinath Devadass - http://techblooms.com
    • Marked as answer by Naheed_A Monday, September 14, 2009 4:17 PM
    Friday, September 11, 2009 7:51 PM

All replies

  • Howdy,

    Here you go:

    private void GetDiscussionItemReplyCounts()
    {
         SPWeb web = new SPSite("http://siteurl").OpenWeb();
    
         using (web)
         {
             SPList disList = web.GetList(web.Url + "/Lists/Duscussion Board");
    
             foreach (SPListItem item in disList.Items)
             {
                 //item - is a discussion in your list
    
                 //Replies count for current item
                 int replyCount = item.ListItems.Count;
             }
    
         }
    }

    Please let me know, If you need more help.

    Thanks,


    Gopinath Devadass - http://techblooms.com
    • Marked as answer by Naheed_A Monday, September 14, 2009 4:17 PM
    Friday, September 11, 2009 7:51 PM
  • Hi,

    You should use the Folder property of the SPQuery class (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.folder.aspx) to restrict to restrict the CAML query to the folder.

    foreach (SPListItem folderItem in list.Folders)
    {
      SPQuery query = new SPQuery();
      query.Folder = folderItem.Folder;
      SPListItemCollection coll = list.GetItems(query);
      count = coll.Count;
    }

    Peter
    • Marked as answer by Naheed_A Monday, September 14, 2009 4:17 PM
    Friday, September 11, 2009 7:52 PM