Working with SharePoint’s Discussion Lists Programmatically – Part 2

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.

转载于:https://www.cnblogs.com/ahghy/archive/2012/11/21/2780445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值