Tuesday, March 3, 2009

Iterating through Sharepoint Discussion List Replies

Hi,

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 List

> Discussion - 1

-->Message 1

-->Message 2

> Discussion - 2

-->Message 1
-->Message 2


> Disccusion - 3

-->Message 1
-->Message 2





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,


3 comments:

Anonymous said...

This is clever, this is the longest something has taken me to resolve in Sharepoint, and still with your help. Thanks.

Anonymous said...

Hi!
Thanks.

but where do we use :
int rootDiscussionThreadId = ListItem["ParentFolderId"].ToString();

Anonymous said...

Hi,
Thanks for this post, it is very much useful for the biginers.