Recently i had been working extensively in the integration of sharepoint discussion list and the document library.
I had one requirement where i have to iterate through the replies of a sharepoint discussion thread, i could hardly find any posts related to that.
First of all Sharepoint discussion lists are very different then other sharepoint lists. It is very different becase it stores the data differently. If you look at the settings of discussion list you will find that it contains two content types
1. Discussion Content Type
2. Messages Content Type
If you digg further you will find that discussion content type is derived from sharepoint folder content type and messages content type is derived from item content type.
Each message(reply) in the discussion list contains a field called ParentFolderId. This field is hidden from the new/edit/view forms of discussion list.
This field always contains id of the root folder for this message (e.g. id of the root discussion thread)
so if you want to get a reference of root discussion thread(aka folder), you can use following piece of code(considering that you have reference to the message)
int rootDiscussionThreadId = ListItem[“ParentFolderId”].ToString();
So when you start a new discussion, internally sharepiont creates a folder withthe subject of the discussion, moving forward all the replies to the root discussion thread are nothign but items created inside the root discussion thread.
> Discussion – 1
> Discussion – 2
> Disccusion – 3
So if you iterate through all the items in the sharepoint list using he normal SPList.Items property, you will get reference of all the root folders (not the replies.)
Even when you navigate to flatView.aspx for any discussion thread, or any other page related to discussion thread you will always find a query string parameter named rootFolder, this query string parameter holds the unique id of the discussion folder.
Getting Replies of a Discussion
If you are in a situation where you have to loop through replies of a perticular discussion thread, you can use folowing code (assuming that you have obtained parent folder / reference of discussion item)
SPQuery queryText = new SPQuery();
queryText.Query = <>;
queryText.Folder = discussion.Folder; //IMP: setting the folder will get items from that perticular folder
SPListItemCollection relatedDiscussions = discussion.ParentList.GetItems(queryText);
Please note that since we want to query items from a perticular folder of the discussion list, we are setting the folder property of SPQuery object to the folder that we want to query. Since my discussion is nothign but a folder in the discussion list, i am setting the folder using that discussion object.
When this query is executed, relatedDiscussion collection will contain list of replies for the given discussion folder,