We can reply to a SharePoint Discussion Thread programmatically using C# code. Here is the code snippet to do this:
using (SPSite objSite = new SPSite("<Site URL>"))
{
SPWeb objWeb = objSite.OpenWeb("<Web Name>");
objWeb.AllowUnsafeUpdates = true;
SPList objList = objWeb.Lists["<Discussion List Name>"];
//objList.Folders returns all the discussion threads in the list.
//Loop through all the discussions and check for discussion thread subject in which you have to post a reply.
//You can also use SPQuery to find the list item in which you have to post a reply.
foreach (SPListItem discussionSubject in objList.Folders )
{
strSubject = discussionSubject.Name;
if(strSubject.Contains(“<Test subject to reply>”))
{
//Get the SPListItem for that discussion subject thread.
SPListItem parentItem = objList.GetItemById(discussionSubject.ID);
//Create reply to that discussion thread subject.
SPListItem reply = SPUtility.CreateNewDiscussionReply(parentItem);
reply["Body"] = "Posting a reply programmatically";
reply.Update();
}
}
objWeb.AllowUnsafeUpdates = false;
}