Programmatically create a reply for the discussion topic in SharePoint 2010


I have a Discussion list in which few topics are created.

I need to create a reply for the discussion topic using SharePoint object model.

Code Snippet:

 

    

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using System.Xml;

using Microsoft.SharePoint.Utilities;

 

namespace CreateReplyforDiscussionTopic

{

    class Program

    { 

        public static void Main(string[] args)

        {

            using (SPSite site = new SPSite("http://serverName/sites/VJTesting/"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists.TryGetList("Team Discussion");

                    if (list != null)

                    {

                        // Get the discussion topic.
SPListItem topic = list.GetItemById(9);

                        // Add the Namespace Microsoft.SharePoint.Utilities for SPUtility class.

                        SPListItem reply = SPUtility.CreateNewDiscussionReply(topic);

                        // In the "body" column you can add the HTML content so that you can add images and add the styles for the contents.

                        // To get the HTML content - Go to the "Discussion" list, then click on "Add new Discussion".

                        // In the body column add the contents and images.

                        // In the ribbon interface, click on "Editing Tools" and then click on "Format text" tab.

                        // In the "Markup" group you could see "HTML" option.

                        // Click on the down arrow and then click on "Edit HTML Source".

                        // Copy the HTML source.

                        reply["Body"] = "<div class='ExternalClassF7F26CE68055477797898F1D2250E309'><div class='ExternalClass37FC140BAA764AAD9DAB0F050FD45712' style='color: #ec008c; text-decoration: underline'><p><strong>&quot;My Reply.&quot;<img alt='DISCUSS.GIF' src='/sites/VJTesting/SiteAssets/Lists/Team%20Discussion/EditForm/DISCUSS.GIF' style='margin: 5px'/><br/><br/></strong>?</p></div></div>";

                        reply.Update();

                        System.Console.WriteLine("Replied for the topic : "+topic.Title);

                        System.Console.ReadLine();

                    }

                    else

                    {

                        System.Console.WriteLine("List does not exists in the site");

                        System.Console.ReadLine();

                    }

                }

            }          

        }

    }

}

 

 

               

 



Reply will be created successfully for the discussion topic.