Thursday, March 29, 2012

Post a reply on SharePoint Discussion Thread programmatically using C# code

01 using (SPSite objSite = new SPSite("<Site URL>"))
02{
03  SPWeb objWeb = objSite.OpenWeb("<Web Name>");
04 
05  objWeb.AllowUnsafeUpdates = true;
06 
07  SPList objList = objWeb.Lists["<Discussion List Name>"];
08 
09  //objList.Folders returns all the discussion threads in the list.
10  //Loop through all the discussions and check for discussion thread subject in which you have to post a reply.
11  //You can also use SPQuery to find the list item in which you have to post a reply.
12  foreach (SPListItem discussionSubject in objList.Folders )
13 {
14    strSubject = discussionSubject.Name;
15 
16    if(strSubject.Contains(“<Test subject to reply>”))
17    {
18      //Get the SPListItem for that discussion subject thread.
19      SPListItem parentItem = objList.GetItemById(discussionSubject.ID); 
20 
21      //Create reply to that discussion thread subject.
22      SPListItem reply = SPUtility.CreateNewDiscussionReply(parentItem);
23      reply["Body"] = "Posting a reply programmatically";
24      reply.Update();
25    }
26  }
27  objWeb.AllowUnsafeUpdates = false;
28}

No comments:

Post a Comment