Working with SharePoint’s Discussion Lists Programmatically – Part 3

Working with SharePoint’s Discussion Lists Programmatically – Part 3

Posted Saturday, May 08, 2010 11:10 PM by Itay Shakury

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

In the previous parts we talked about how SharePoint’s Discussion lists work, and how to work with the from server side.
In this post we will see how to work with them from client side, using the managed Client Object Model.

Getting all Posts

ClientContext ctx = new ClientContext("http://dev-sp2010-01/sites/itaysk/forum");

//Get the Discussion list
List lst = ctx.Web.Lists.GetByTitle("Team Discussion");
//Get the topics in the list
CamlQuery q = CamlQuery.CreateAllFoldersQuery();
ListItemCollection topics = lst.GetItems(q);
ctx.Load(topics);
ctx.ExecuteQuery();

To get all the topics (folders), we are basically asking for all the folders in the list.

Getting all Replies

//Select a topic
ListItem topic = topics[0];
//Get the replies of the selected topic
q = CamlQuery.CreateAllItemsQuery(100, "Title", "FileRef", "Body");
//FileRef contains the site relative path to the folder
q.FolderServerRelativeUrl = topic["FileRef"].ToString();
ListItemCollection replies = lst.GetItems(q);
ctx.Load(replies);
ctx.ExecuteQuery();

First we select a topic (Folder). Then we ask for all the items in that folder.

 

CamlQuery q = new CamlQuery();
//Find all replies for topic with ID=1
q.ViewXml = @"<View Scope='Recursive'>
<Query>
<Where>
<Eq>
<FieldRef Name="
"ParentFolderId"" />
<Value Type="
"Integer"">1</Value>
</Eq>
</Where>
</Query>
</View>"
;
ListItemCollection replies = lst.GetItems(q);
ctx.Load(replies);
ctx.ExecuteQuery();

Here we are using the “ParentFolderId” column, that every reply has.

Creating a Topic

As I explained in Part 2, topic and reply creation are a bit more complicated because we have to take care of Threading that’s why we have special functions to do that.

ClientContext ctx = new ClientContext("http://dev-sp2010-01/sites/itaysk/forum");

//Get the Discussion list
List lst = ctx.Web.Lists.GetByTitle("Team Discussion");

//Create the topic
ListItem t = Microsoft.SharePoint.Client.Utilities.Utility.CreateNewDiscussion(ctx, lst, "Creted by Client OM");
t["Body"] = "This is the body";
t.Update();
ctx.ExecuteQuery();

Creating a Reply

Again, using the proprietary function:

//Get the topic for which we eant to reply (assuming we already got the topic list into "topics")
//You can also get the topic in ther ways.
ListItem t = topics[0];
//Create the reply
ListItem r = Microsoft.SharePoint.Client.Utilities.Utility.CreateNewDiscussionReply(ctx, t);
r["Body"] = "This is the reply body";
r.Update();
ctx.ExecuteQuery();

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 have seen how to work with discussion lists from the client, using Client OM.
This post concludes the series, hope I helped.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值