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

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.

תגים:,

Comments

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

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

# Working with SharePoint&#039;s Discussion Lists Programmatically ??? Part &#8230; | Working Gloves

Pingback from  Working with SharePoint&#039;s Discussion Lists Programmatically ??? Part &#8230; | Working Gloves

# Working with SharePoint&#39;s Discussion Lists Programmatically ??? Part &#8230; Blog

Pingback from  Working with SharePoint&#39;s Discussion Lists Programmatically ??? Part &#8230; Blog

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

Sunday, August 22, 2010 2:32 PM by Shachar

The real problem is to create a reply to reply - anybody knows how to to it?

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

Monday, September 27, 2010 12:16 PM by Paul

Great post mate and really help, thanks for saving me a lot of time in research.

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

Wednesday, December 22, 2010 11:05 AM by brijesh

it's very useful post. thanks.

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

Thursday, December 23, 2010 9:58 AM by Raghu

many many thanks..very insightfull

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

Tuesday, February 22, 2011 5:34 PM by Martin

Hi,

I'm trying to iterate all replies. It would really help if you give some more info about ForumDataContext and ClientContext classes.

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

Thursday, June 30, 2011 4:48 PM by MG

Great Article! Helped me a lot. However, I am stuck with an issue and I wonder if you would know how to get it resolved.

The body content that I am writing into the discussion list is HTML content that has an embedded image.

Do you how embedded images are handled in SharePoint.

What would I need to do in order make this work?

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

Tuesday, September 27, 2011 8:31 AM by Prajith

Hi Dear friend,

Its a wondefull post. presented in a very simple manner. Congratz !!!

Leave a Comment

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

Enter the numbers above: