Thursday, February 18, 2010

How to reply on discussion thread programmatically – Part 2

Hi All,

I am back with more details on discussion thread reply programmatically. In part 1, we simply went through how we can create discussion list and create simple reply.

This post will tell you more details regarding it.

Let us start with replying individual thread reply. First you need to loop through the subjects or best it to query discussion list with the help of SPQuery on subject field, so that you get the SPListItem object in your hand to play with.

using (SPSite objSite = new SPSite("{site URL}"))
{
SPWeb objWeb = objSite.OpenWeb("Web Name");

objWeb.AllowUnsafeUpdates = true;

SPList objList = objWeb.Lists["{discussion list name}"];

//remember objList.Folders will return you all discussion subjects. objList .ItemCount will return you all discussion along with their replies, objList .Items.count will return only replies. So now we will loop through all subjects,for demo I have also three discussion threads in my list. In real scenario, query the list and get SPListItem object.
foreach (SPListItem lstsubject in objList.Folders )
{

strSubject = lstsubject.Name;

if(strSubject.Contains(“test subject to reply”))
{
SPListItem parentitem = objList.GetItemById(lstsubject.ID); //Get //the SPListItem for that discussion subject thread.

//Create reply to that subject.

SPListItem reply = SPUtility.CreateNewDiscussionReply(parentitem);

reply["Body"] = "Yippi…This is my reply programmatically";
reply.Update();
}
}
objWeb.AllowUnsafeUpdates = false;
}


Hope this will help you a bit. In my part 3, I will explain bit more about how to go ahead and reply individual thread inside perticular subject.

2 comments:

Micah said...

Is part 3 out yet? I need to know how to post replies to replies, not just replies to topics.

Thanks.

SharePoint Kings said...

Micah,
just check if replies to reply is available by default only then it can be achieved by code.

its quite time we worked on discussion :(




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com