Programmatically get the discussion topics in SharePoint 2010


In this you will see how to get only the discussion topics using SharePoint object model.

I have a Discussion list named "Team Discussion" in which i have created two topics and each topic has few replies.

I want to retrieve only the two topics using SharePoint object model.

Code Snippet:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using System.Xml;

 

namespace GetDiscussionTopics

{

    class Program

    { 

        public static void Main(string[] args)

        {

            using (SPSite site = new SPSite("http://serverName/sites/VJTesting/"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists.TryGetList("Team Discussion");

                    if (list != null)

                    {

                        // Empty Query is passed to get only the discussion topics

                        SPListItemCollection itemColl = list.GetItems(new SPQuery());

                        foreach (SPListItem item in itemColl)

                        {

                            System.Console.WriteLine(item["Title"].ToString());

                            System.Console.WriteLine(item["Replies"].ToString());

                        }

                        System.Console.ReadLine();

                    }

                    else

                    {

                        System.Console.WriteLine("List does not exists in the site");

                        System.Console.ReadLine();

                    }

                }

            }          

        }

    }

}

 

 



Using the above code snippet you will be able to get only the discussion topics.