Tuesday, February 10, 2015

How to Get all Replies in Discussion board using CAML

Referred URL - http://geekswithblogs.net/shehan/archive/2009/10/10/get-all-replies-to-a-discussion.aspx

A discussion list in unlike a normal list in that it consists of two content types  - a Discussion content type that maps to the discussion topic and a Message that maps to all replies to the topic. If you examine the Discussion content type you’ll see that its inherited from the Folder content type.
The reply contains a field called ‘ParentFolderId’ and as the name suggests it holds the ID of the discussion topic. So naturally if you were to do a query on the list for all items with the same ParentFolderId you would expect the query to work. However, since the topic is of a folder content type and the replies are contained within the ‘folder’ you would need to set the  queries ViewAttributes property to "Scope='Recursive'" for it to return results.
Below is an example using the object model:
      SPQuery query = new SPQuery();
query.ViewAttributes = "Scope='Recursive'";
StringBuilder queryString = new StringBuilder();
queryString.Append("         ");
queryString.Append("              ");
queryString.Append("                        ");
queryString.Append("                             ");
queryString.Append("                             Message");
queryString.Append("                        
");
queryString.Append("                        ");
queryString.Append("                             ");
queryString.Append("                             99"); //NOTE: change this to the value you need
queryString.Append("                        
");
queryString.Append("              
");
queryString.Append("         
");
query.Query = queryString.ToString();
DataTable replyTable = discussionList.GetItems(query).GetDataTable();




No comments: