Programmatically create a discussion topic in SharePoint 2010

I have a Discussion list in which i need to add a new 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 CreateNewDiscussionTopic

{

    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)

                    {

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

                        SPListItem newTopic = SPUtility.CreateNewDiscussion(list, "Topic5");

                        // 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.

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

                        newTopic.Update();

                        System.Console.WriteLine(newTopic.Title + " discussion topic is created successfully");

                        System.Console.ReadLine();

                    }

                    else

                    {

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

                        System.Console.ReadLine();

                    }

                }

            }          

        }

    }

}

 





Ebook Download
View all
Learn
View all