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

Comments (8)

  1. commodityfever says:

    """""""It was a awe-inspiring post and it has a significant meaning and thanks for sharing the

    information.Would love to read your next post too......"""""""

    Thanks

    sonictrades.com/intraday-stock-tips

    sonictrades.com/intraday-nifty-futures-tips

  2. vijji says:

    Can I get an explanation of what is happening. There are hardly any comments. I am not able to understand what you are doing and why u r doing?

  3. Nice and informative article. Thanks foe sharing.

  4. Bilal says:

    Good work Pranab..

    Just wanted to confirm a behaviour as I was unable to do it.

    Are you able to connect this discussion board to outlook and post messages from outlook?

    I implemented a Custom Discussion Board definition. It was successfully connecting to Outlook but the communication was one way. I was unable to post messages from Outlook. It works fine for Out of the Box Discussion boards.

    You may reply me back at mailtobilalahmad@yahoo.com

    Thanks,

    -Bilal

  5. hi thankx says:

    do u know

    how to get replies for each discussion thru code..?

  6. nareshveeragoni says:

    Hi, Pranab, really very good article searching abougt this kind of development from a long while. I have a problem in deploying this solution. i can build with out any errors but while trying to delploy I amgetting this error from the schema.xml file saying

    "The 'Override' attribute is not declared."

    <Query>

    OrderBy Override="TRUE">

    <FieldRef Name="Threading">

    </FieldRef>

    </OrderBy>

    </Query>

    so I am not sure about resolving this error or is there any way to add this solution from the build. if it is what kind of steps i need to perform.

    can you please reply me to nveeragoni@gmail.com.

    much appriciate your response.

    Thanks.

  7. Rajiv Giri says:

    It was really an useful stuff. But i have a document library and on its itemadded event want to create a new file in a particualr list (Task) and want to modify the existing metadata/custom-column. But after the update list page doesnot show anything.... Please suggest some valid API to create a dynamic file in List based on event handler of Document Library ...

  8. Requirement: This is an enhancement of the List Definition project we have created in previous post (

Skip to main content