Working with SharePoint's discussion lists programmatically-Part 2

Source: Internet
Author: User
Working with SharePoint's discussion lists programmatically-Part 2

Posted Wednesday, May 05,201 0 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 essential 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 cocould 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 mechanic 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 can reply.

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.