Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Programmatically Read Discussion Board & Replies

Posted by Jean Paul on October 21, 2014

I searched a lot for Server Object Code in reading Discussion Board Items & Replies. As cannot find one, following is created.


You need to programmatically get all discussion items along with their replies.


This is our Discussion Board.


This is the code to retrieve all discussion items.

using (SPSite site = new SPSite(“http://hpvm”))


using (SPWeb web = site.OpenWeb())


SPList list = web.Lists.TryGetList(“Discussion Board”);

if (list != null)


SPQuery query = new SPQuery();

query.ViewFields = “<FieldRef Name=’Title’ />”;

SPListItemCollection itemColl = list.GetItems(query);

foreach (SPListItem item in itemColl)







Make sure the code is returning values like this.


Now we are writing the code to return the replies.

private static void GetReplies(SPList list, SPListItem item)


SPQuery query = new SPQuery();

query.Folder = item.Folder;

SPListItemCollection collection = list.GetItems(query);

SPListItem replyItem = collection[0];

string body = (string)replyItem[“Body”];

string author = (string)replyItem[“Author”];




Following is the output on running the console application.


You can see the Author is returned mixing with ID. You can use the following method to extract the correct User Name from the string.

private static string GetUserName(string author, SPList list)


string result = author;

if (author.Contains(“;”))


int id = int.Parse(author.Split(‘;’)[0]);

SPUser user = list.ParentWeb.SiteUsers.GetByID(id);

result = user.Name;


return result;


Now the user name seems to be better.



I am creating a Console Application to retrieve the same.



In this article we have explored how to fetch Discussion Board Items & Replies. The source code is attached here with the article.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s