SharePoint Discussion Board Common Operations through Code

5. November 2008

1) Take instance of your site collection and web:

SPSite currentSite = SPContext.Current.Site;
SPWeb currentRootWeb = currentSite.RootWeb;

2) Suppose we have the name of discussion list - "MyDiscussionList".

Get GUID of this list:

Guid myDiscussionListGUID = Guid.NewGuid();

foreach (SPList list in currentRootWeb.Lists)

{

if (list.BaseTemplate.ToString() == "DiscussionBoard" && list.Title == "MyDiscussionList")

{

myDiscussionListGUID = list.ID; // Read GUID of Discussion List

break;

}

}

Tips:

a) list.ItemCount will return all discussions and their replies

b) list.Items.Count will return only replies

3) Get your list:

SPList myDiscussionList = currentRootWeb.Lists.GetList(myDiscussionListGUID, false);

4) Create a new Discussion:

SPListItem newItem = SPUtility.CreateNewDiscussion(myDiscussionList.Items, "New Message using code");
newItem["Body"] = "My new message content using code";
newItem.Update();

5) How to read all discussions:

foreach (SPListItem folder in myDiscussionList.Folders)
{
Response.Write("Folder Name: " + folder.Name + "<BR>");
Response.Write("Folder ID: " + folder.ID + "<BR>");
Response.Write("Attachments Count: " + folder.Attachments.Count + "<BR>"); // Returns attachment count


// Code to read attachment URL.

for (int i = 0; i < folder.Attachments.Count; i++)
{
Response.Write("Attachment Url " + folder.Attachments.UrlPrefix + folder.Attachments + "<BR>");
}

// Read body of attachment

Response.Write("Body: " + folder.Fields["Body"].GetFieldValueAsText(folder["Body"]) + "<BR>");
}

6) If you want to delete a discussion:

SPListItem listItemParentToDelete = null;

foreach (SPListItem folder in myDiscussionList.Folders)
{
listItemParentToDelete = folder;

}

if (listItemParentToDelete != null)
{
listItemParentToDelete.Delete();
}


7) Loop through all discussion replies:

foreach (SPListItem listItem in myDiscussionList.Items)

{

Response.Write("Item DisplayName: " + listItem.DisplayName + "<BR>"); // Returns Title of Discussion
Response.Write("List ID: " + listItem.ID + "<BR>");
Response.Write("List Folder ID: " + listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]).ToString() + "<BR>"); // Returns ID of Parent Discussion
Response.Write("Body: " + listItem.Fields["Body"].GetFieldValueAsText(listItem["Body"]) + "<BR>");

// Create Parent List Item
int parentListID = Convert.ToInt32(listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]));
SPListItem parentListItem = lvContentItemsDiscussionsList.GetItemById(parentListID);
Response.Write("Parent List Item Name: " + parentListItem.Name + "<BR>");

// Code to Reply to a Discussion Message
SPListItem reply = SPUtility.CreateNewDiscussionReply(parentListItem);
reply["Body"] = "<div class=ExternalClass89C47CD7892B4279A8F42A65DD63AE3A><div> </div> <div>Reply to the new message<br><br> <hr> <b>From: </b>Admin<br><b>Posted: </b>Friday, July 20, 2007 4:01 AM<br><b>Subject: </b>New message<br><br> <div class=ExternalClass3D04672E599B486F9ECB76C138494708> <div>My new message content</div></div></div></div>";
reply["TrimmedBody"] = "<div class=ExternalClass677134B4EA284660B1B236824800345C><div> </div> <div>Reply to the new message<br></div></div>";
reply.Update();


// Code to delete a discussion reply
listItemToDelete = listItem;

}


8) Code to delete a discussion reply:

SPListItem listItemToDelete = null;

foreach (SPListItem listItem in myDiscussionList.Items)

{

listItemToDelete = listItem;

}

// Code to delete a discussion reply
if (listItemToDelete != null)
{
listItemToDelete.Delete();
}

Note: You need to allow unsafe updates in order to do any operation on SharePoint list directly.

currentRootWeb.AllowUnsafeUpdates = true;

Hope this helps!

SharePoint

Comments

2/28/2010 9:57:09 PM #
Should I get a VPS? Currently I am using ixwebhosting but they keep disabling my account due to high server load. Im getting about 4,000 unique views a day. What brand should I get?
3/24/2010 12:59:54 PM #
And if you walk away, i know that it will be the best thing to ever walk out of my life.[CHECK THIS WEBSITE!!!: http://dukeware.com/ ]
4/28/2010 5:42:32 AM #
i like the layout of your blog. hope you don't mind if i do something similar with mine.
Mark
Mark
5/17/2010 9:15:37 PM #
Ok. This is great information on this subject.
Peace
5/29/2010 1:36:35 PM #
I have been to your site before. The more I visit, the more I keep coming back!
6/20/2010 9:12:16 AM #
This is actually the second moment I have discovered your webblog in the last few weeks.  Looks like I ought to bookmark it.
6/23/2010 3:05:21 PM #
Excellent blog post, loads of useful information.
6/30/2010 1:43:31 AM #
Awesome blog, info is great!  HDMI is BY FAR the best way to go!
8/9/2010 3:07:22 AM #
I wished to thanks for this great read!! I definitely loved every little bit of it. I have you bookmarked your website to check out the brand new stuff you post.If you happen to're all for having a visitor weblog poster please reply and let me know. I will offer you distinctive content for your webstie, thanks.
8/10/2010 11:12:30 PM #
Can I quote you on my website if I link again to your web site?
8/12/2010 1:59:57 PM #
Greetings
I find your opinion interesting in your article SharePoint Discussion Board Common Operations through Code. I will certainly be reading your blog more.
See you
North American

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading