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/p/2780447.html