Another thing I’d not done through the SharePoint Object Model was create or reply to a Discussion. This sounded a bit more complicated, but I found a helpful post of ‘Common Discussion Operations‘…
My Code was:
//** Create a Discussion thread
SPList disc = site.Lists["Discussion"];
SPListItem topic = SPUtility.CreateNewDiscussion(disc.Items, "Test Thread");
topic["Body"] = "Lorem Ipsum and so on";
topic.Update();
//** Create a reply
SPListItem reply = SPUtility.CreateNewDiscussionReply(topic);
reply["Body"] = "This is, like, a reply, dude!";
reply.Update();
This code will create a new thread, and create a single reply to it. Once you’ve got the SPUtility object, it’s all much simpler than I’d expected.
The SPUtility object is found in the Microsoft.SharePoint.Utilities namespace – all sorts of things tucked away in there.
Hi Andy
When I use your code to create a new reply I get the error below even though it still adds the reply
———————————————————————————–
System.InvalidOperationException: Operation is not valid due to the current state of the object. at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context) at Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context) at Microsoft.SharePoint.SPContext.get_Current() at Microsoft.SharePoint.SPListItem.UpdateDiscussionRoot(SPSite impersonationSite, Int32 parentFolderId) at Microsoft.SharePoint.SPListItem.c__DisplayClass2.b__0() at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state) at Microsoft.SharePoint.SPSecurity.c__DisplayClass4.b__2() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPListItem.DirtyListItemAfterUpdate(Boolean failed, Boolean bAdd, Boolean bMigration, Int32 ulID, Int32 parentFolderId) at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) at Microsoft.SharePoint.SPListItem.Update() at Test_DiscBoard_ucDiscBoardAddReply.cmdReply_Click(Object sender, EventArgs e) in C:Usersts3045DocumentsVisual Studio 2008WebSitesucListUserswpTestDiscBoarducDiscBoardAddReply2.ascx.vb:line 29
Operation is not valid due to the current state of the object.
———————————————————————————–
Please any advice will be appreciated.