SharePoint 2007 (MOSS/WSS) – How to add a new column in Discussion Board showing the Latest Thread for each Discussion – Step by Step

Step 1:

Start VS 2008 and select Visual C# > SharePoint. Select List Definition as the project template. Rename the project as CustomDiscussionBoard and click OK. In the List Definition Setting dialog box click OK.

Step 2:

In the solution explorer delete the ListDefinition1 folder and right click on the project and choose Add>New Item>List Definition and change the name from ListDefinition1 to CustomDiscussionBoard. In the List Definition Setting dialog box select Discussion Board and select Add with event receiver.

Step 3:

Under CustomDiscussionBoard folder click on ItemEventReceiver.cs file to open it. Just before the default constructor add this line:

string bodytext = "";

Step 4:

Uncomment the ItemAdded method and add the following lines there:

bodytext = properties.ListItem["Body"].ToString();

            if (bodytext != "")

            {

                int startpos = bodytext.IndexOf("<div>") + 5;

                int endpos = bodytext.IndexOf("</div>") - startpos;

                string[] strArr = bodytext.Substring(startpos, endpos).Replace("<br>", "").Split(new Char[] { ' ' });

                if (strArr.Length > 2)

                {

                    properties.ListItem["LatestThread"] = strArr[0] + " " + strArr[1] + " " + strArr[2] + "....";

                }

                else if (strArr.Length == 2)

                {

                    properties.ListItem["LatestThread"] = strArr[0] + " " + strArr[1];

      }

                else

                {

                    properties.ListItem["LatestThread"] = strArr[0];

                }

                properties.ListItem.Update();

                if (properties.ListItem.Folder != null)

                {

                    string[] strArr2 = bodytext.Substring(startpos, endpos).Replace("<br>", "").Split(new Char[] { ' ' });

                    if (strArr2.Length > 2)

                    {

                        properties.ListItem.Folder.Properties["LatestThread"] = strArr2[0] + " " + strArr2[1] + " " + strArr2[2] + "....";

                    }

                    else if (strArr2.Length == 2)

                    {

                        properties.ListItem.Folder.Properties["LatestThread"] = strArr2[0] + " " + strArr2[1];

                    }

                    else

                    {

                        properties.ListItem.Folder.Properties["LatestThread"] = strArr2[0];

                    }

                    properties.ListItem.Folder.Update();

                }

            }

Step 5:

Uncomment the ItemUpdated method and add the following lines there:

SPList list = (SPList)properties.ListItem.ParentList;

            SPQuery query = new SPQuery();

            query.Folder = properties.ListItem.Folder;

            SPListItemCollection listItemCol = list.GetItems(query);

            foreach (SPListItem item in listItemCol)

            {

                bodytext += item["Correct Body To Show"].ToString();

            }

            if (bodytext != "")

            {

                int lastclasspos = bodytext.LastIndexOf("<div class") + 4;

                int lastclosedivpos = bodytext.LastIndexOf("</div>");

                string newbodytext = bodytext.Substring(lastclasspos, lastclosedivpos - lastclasspos);

                int startpos = newbodytext.IndexOf("<div>") + 5;

                int endpos = newbodytext.IndexOf("</div>") - startpos;

                string[] strArr = newbodytext.Substring(startpos, endpos).Replace("<br>", "").Split(new Char[] { ' ' });

                if (strArr.Length>2)

      {

            properties.ListItem["LatestThread"] = strArr[0] + " " + strArr[1] + " " + strArr[2] + "....";

      }

                else if (strArr.Length == 2)

                {

                    properties.ListItem["LatestThread"] = strArr[0] + " " + strArr[1];

                }

                else

                {

                    properties.ListItem["LatestThread"] = strArr[0];

                }

                properties.ListItem.Update();

            }

Step 6:

Under CustomDiscussionBoard folder click on schema.xml file to open it. Find the <Fields> tag, under it after the first <Field></Field> tag, add the following tag:

<Field ID="{5915B79B-4218-4056-B8CA-E51BC8A5ED14}" Type="Text" Name="LatestThread" DisplayName="LatestThread" StaticName="LatestThread">

                  </Field>

Create a new GUID using GUIDGen and change the GUID in the ID attribute above.

Step 7:

Right click on the project in Solution Explorer and select properies. In the properties window go to Debug and in the Start Browser with URL option add the URL of your destination site. Now Build the site and deploy it from the Build menu.

Step 8:

Open the destinantion site and go to Site Actions>Create. You will find CustomDiscussionBoard under Communications section. Click on it and create a discussion board called testdisc. Once created and opened go to Settings and Discussion Board Settings. Under Views section select Flat. In the next window make sure Make this default view option is selected and select LatestThread column from the Columns section and click OK. Do the same with the Subject view too. Once done you can create new discussions and add replies to those discussions. You can see the LatestThread column will show the last thread value. If you have more than 3 words in the content it will show periods after 3 words when it will show the column.

Also see SharePoint 2007 (MOOS/WSS) – How to add a new column (Custom BDC Column) in Discussion Board (Part2) – Step by Step