Thursday, January 1, 2009

Get SharePoint Discussion Forum Replies Data

This is a sample code how to get all discussion replies data in sharepoint discussion forum.

1:  SPWeb web = SPContext.Current.Web;
2:  SPList listForum = web.Lists["Forum"];
3:   
4:       /* Get the Discussion Forum Data */
5:       SPQuery qry = new SPQuery();
6:       qry.Query = @"<OrderBy>
7:                         <FieldRef Name='DiscussionLastUpdated' Ascending='False' />
8:                    </OrderBy>";
9:       qry.RowLimit = 3;
10:   
11:      SPListItemCollection itemsColl = listForum.GetItems(qry);
12:   
13:      if (itemsColl.Count > 0)
14:      {
15:         foreach (SPListItem item in itemsColl)
16:         {  
17:            Response.Write("ID: " + item.ID + "<BR>");
18:            Response.Write("Name: " + item.Name + "<BR><BR>");
19:              
20:            /* Get all discussion replies */
21:            SPQuery qry2 = new SPQuery();
22:            qry2.Query = @"<OrderBy>
23:                              <FieldRef Name='DiscussionLastUpdated' Ascending='False' />
24:                           </OrderBy>";
25:   
26:            qry2.Folder = item.Folder;
27:            SPListItemCollection itemsColl2 = listForum.GetItems(qry2);
28:                
29:            if (itemsColl2.Count > 0)
30:            {
31:              foreach (SPListItem replayItem in itemsColl2)
32:              {
33:                Response.Write("Display Name: " + replayItem.DisplayName + "<BR>");
34:                Response.Write("List ID: " + replayItem.ID + "<BR>");
35:                Response.Write("List Folder ID: " +
replayItem["Parent Folder Id"].ToString() + "<BR>");
36:                Response.Write("Body: " + replayItem["Body"] + "<BR>");
37:                Response.Write("Last Replay By:" + replayItem["Author"].ToString().
Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries)[1]
+ "<BR><BR>");
38:              }
39:           }
40:           else
41:           {
42:               Response.Write("No Replay Data <BR><BR>");
43:           }
44:        }
45:     }

No comments: