DCSIMG
Working with SharePoint’s Discussion Lists Programmatically – Part 2 - itaysk

Working with SharePoint’s Discussion Lists Programmatically – Part 2

Posted Wednesday, May 05, 2010 3:43 PM by Itay Shakury

This is part of a series of posts about Working with Discussion lists programmatically: Part 1, Part 2 (this one), Part 3.

In Part 1, I have introduced the structure and inner workings of SharePoint Discussion Lists. Now, Lets take a look of some code samples with SharePoint’s Object Model, and LINQ 2 SharePoint.

Getting all Posts

ForumDataContext dc = new ForumDataContext("http://dev-sp2010-01/sites/itaysk/Forum");

//Get all topics in the Discussion list
IEnumerable<Discussion> topics = from t in dc.TeamDiscussion
select (Discussion)t;

We ask for all the list items that are in the root of the list. Those are essentially the items of type “Discussion” which are folders.

Alternatively, you can use SPList.GetItems(SPQuery query) to get the items. An empty query will do because the list contains only topics at it’s root. (You could filter for Folder content type if you like too, but it’s not necessary)

SPList list = web.Lists["Team Discussion"];
SPListItemCollection res = list.GetItems(new SPQuery());

Getting all Replies for a post

//select a single discussion (in this case, the first one), to view it's content
Discussion topic = (Discussion)dc.TeamDiscussion.Single(t => t.Id == 5);

//Get all the replies for the selected discussion
IEnumerable<Message> replies = from reply in dc.TeamDiscussion.ScopeToFolder("/"+topic.Reply+"/"+topic.Title, false)
select (Message)reply;

After selecting a specific post (folder), we are asking for all the list items that are in that folder.

Alternatively, if you prefer to work with CAML, you can use this CAML query that asks for all items in a folder:

SPList list = web.Lists["Team Discussion"];
//Get the topic. (you can use other ways to get the topic)
SPFolder t = list.GetItemById(1).Folder;
SPQuery q = new SPQuery();
q.Folder = t;
SPListItemCollection res = list.GetItems(q);

Or use this one, that uses the “ParentFolderId” column that every reply has.

SPList list = web.Lists["Team Discussion"];
//This Query gets all items of the topic with ID=1
string strQ = @"<Query>
<Where>
<Eq>
<FieldRef Name="
"ParentFolderId"" />
<Value Type="
"Integer"">1</Value>
</Eq>
</Where>
</Query>"
;

SPQuery q = new SPQuery();
//This line makes the query search ib all folders
q.ViewAttributes = "Scope=\"Recursive\"";
q.Query = strQ;
SPListItemCollection res = list.GetItems(q);

Creating a Topic

Don’t be tempted to manually create a regular list item in the list, because it will miss Threading and other stuff the mechanism needs. 
Luckily, we have a special function that does all this for us.

SPList list = web.Lists["Team Discussion"];
SPListItem t = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussion(
list, "Created by Code");
t[SPBuiltInFieldId.Body] = "Created by Code";
t.Update();

Creating a Reply

Again, using the proprietary function.

SPList list = web.Lists["Team Discussion"];
//Get the topic for which we are replying to. (you can also get it in other ways)
SPListItem t = list.GetItemById(11);
SPListItem r = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussionReply(
t);
r[SPBuiltInFieldId.Body] = "Created by Code";
r.Update();

In this example, we have replied to the root of the topic. You can also reply to a specific reply inside the topic – just pass the CreateNewDiscussionReply function the object that you want to reply to.

Conclusion

In this post we learned how to create discussion items, and replies, as well as query them. We have used Server Object Model.

In the next posts I will show how to do the same things from the client.

תגים:,

Comments

# Working with SharePoint???s Discussion Lists Programmatically ??? Part 1 - itaysk

Pingback from  Working with SharePoint???s Discussion Lists Programmatically ??? Part 1 - itaysk

# Twitter Trackbacks for Working with SharePoint???s Discussion Lists Programmatically ??? Part 2 - itaysk [microsoft.co.il] on Topsy.com

Pingback from  Twitter Trackbacks for                 Working with SharePoint???s Discussion Lists Programmatically ??? Part 2 - itaysk         [microsoft.co.il]        on Topsy.com

# re: Working with SharePoint’s Discussion Lists Programmatically – Part 2

Monday, June 07, 2010 7:10 PM by Theo

Hi Itay

Many thanks for your post. When I try to replying, I get the floowing error message BUT it still adds the reply

34System.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:\Users\ts3045\Documents\Visual Studio 2008\WebSites\ucListUserswp\Test\DiscBoard\ucDiscBoardAddReply.ascx.vb:line 27

PLEASE HELP

# re: Working with SharePoint’s Discussion Lists Programmatically – Part 2

Thursday, August 12, 2010 5:26 PM by Vinod kumar

hii,

code given by u to fetch replies related to discussion is not returning any value:

//select a single discussion (in this case, the first one), to view it's content

Discussion topic = (Discussion)dc.TeamDiscussion.Single(t => t.Id == 5);

//Get all the replies for the selected discussion

IEnumerable<Message> replies = from reply in dc.TeamDiscussion.ScopeToFolder("/"+topic.Reply+"/"+topic.Title, false)

                              select (Message)reply;

please help.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: