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> <HR> <B> from: </B> admin <br> <B> posted: </B> Friday, July 20,200 7 am <br> <B> subject: </B> NEW MESSAGE <br> <Div class = externalclass3d04672e599b486f9ecb76c138494708> <div> my new message content </div>> ";
Reply ["trimmedbody"] = "<Div class = externalclass677134b4ea284660b1b21_24800345c> <div> </div> <div> reply to the new message <br> </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;
Original post address: http://www.beyondweblogs.com/post/SharePoint-Discussion-Board-Common-Operations-through-Code.aspx