Szymon Kobalczyk's Blog

A Developer's Notebook

  Home  |   Contact  |   Syndication    |   Login
  106 Posts | 6 Stories | 578 Comments | 365 Trackbacks

News

View Szymon Kobalczyk's profile on LinkedIn

Twitter












Tag Cloud


Article Categories

Archives

Post Categories

Blogs I Read

Tools I Use

The project I currently work on involves accessing SharePoint's Discussion Board from a desktop applications. This includes not only reading discussions but also creating new ones as well as replying to existing posts. Unfortunately as I quickly found out, there is no dedicated web service for dealing with this type of lists similar to Imaging or Meetings. So the only option was to use the the generic Lists web service. However this poses several problems that I want to share.

Reading discussion lists

If you read items from any discussion list using familiar GetListItems method all you will get are the top level discussion topics, similar to the default view if you view such list in browser. But how do you get all other messages that were posted as replies to these discussions?

To answer this question first we need to look at the configuration of such list. 

As you can see this list has two distinct Content Types: Discussion and Message. Let's look at the first one:

Do you notice something odd here? This was actually surprising for me, but it turns out that Discussions are actually Folders! This means that we will probably find the Messages if we look inside particular folder. This makes sense if you think of it.

Ok, so how you read contents of a folder? It also took me while to find but the GetListItems method has additional parameter for QueryOptions. This is also XmlNode but it can contain Folder element that indicates with subfolder you want to read. In this case to get messages from discussion you need to pass the Url of the top level discussion as folder name like this:

XmlDocument doc = new XmlDocument();
XmlElement query = doc.CreateElement("Query");
XmlElement viewFields = doc.CreateElement("ViewFields");
XmlElement queryOptions = doc.CreateElement("QueryOptions");

XmlElement folder = doc.CreateElement("Folder");
queryOptions.AppendChild(folder);
folder.InnerText = discussionUrl;

XmlNode nodeListItems = service.GetListItems(listName, viewName, 
    query, viewFields, null, queryOptions, null);

This will return list of Messages. Basically all you need is the Body, Author and Created date but it has some other useful properties that you can use. For example DiscussionTitle will give you the subject of the discussion thread for the message and IsRootPost is another way to distinguish messages from top level discussions. The Discussion item contains same properties as well as one useful property called ItemChildCount that gives you a number of replies in a thread.

One very important property is Threading because it allows you to construct the threaded view of the discussions. It contains a text string that might seem garbage at first but if you compare these strings for several messages you might notice a pattern here. Basically for each reply this string starts with the Threading property of the parent messages and appends some suffix (I couldn't figure out how this suffix was constructed though). If you sort all messages on the Threading property, you can easily find out at what level the Message should be placed by comparing these string (I can post a simple recursive method to construct a tree view from this if anyone needs it).

Creating discussion items

So how do you add new add new items to the Discussion Board? Let's try with the UpdateListItems method from Lists web service. Following code will add new top level discussion item to specified discussion list.

XmlDocument xmlDocument = new XmlDocument();
XmlElement updatesNode = xmlDocument.CreateElement("Batch");

XmlElement methodNode = xmlDocument.CreateElement("Method");
methodNode.SetAttribute("ID", "0");
methodNode.SetAttribute("Cmd", "New");
updatesNode.AppendChild(methodNode);

XmlElement titleNode = xmlDocument.CreateElement("Field");
titleNode.SetAttribute("Name", "Title");
titleNode.InnerText = subject;
methodNode.AppendChild(titleNode);

XmlElement bodyNode = xmlDocument.CreateElement("Field");
bodyNode.SetAttribute("Name", "Body");
bodyNode.InnerText = body;
methodNode.AppendChild(bodyNode);

using (ListsWebService.Lists service = CreateListService())
{
    XmlNode resultNode = service.UpdateListItems(listName, updatesNode);
}

Ok, this part wasn't hard. But how do I reply to this discussion? And here my friends is where I got stuck right now....

I've tried to use several properties to force the Message into certain Discussion/Folder. Both ParentFolderId and FileDirRef have no effect. The only way I could do it is by using BaseName property. But it requires to specify the name for the new item in advance in form <parent_folder>/<new_item> so there is always risk that it won't be unique (I hoped that SP would generate this for me). Another problem is that even though the new items are placed inside Discussion their Content Type is Discussion instead of Message, so the thread starts looking strange. I've tried setting both ContentType and ContentTypeId but it didn't work either. I've also tried setting the FSObjectType to 0 (zero) as it should indicate this is normal item not a folder.

Another problem I need to solve is how to construct the Threading property for the new item for this message to appear in correct order. I can't figure out how the suffix for the new message is constructed.

If there are any SharePoint specialists out there reading this I would really appreciate your help :-)

Please continue to my second article to learn how I solved these problems.

posted on Monday, March 12, 2007 11:48 AM

Feedback

# re: How to access SharePoint's Discussion Board using Web Services 7/4/2007 9:21 AM Pooja Khunger
We are doing a similar work and need to display all the replies pertaining to a post.
You have mentioned about the threading property. How can we use this to see all the replies and post a new reply to a discusion topic.



# re: How to access SharePoint's Discussion Board using Web Services 7/15/2007 10:36 AM Szymon
Hi,
It turned out that the Threading property doesnt help much here. Please read the second part of my post (link above) to learn how I solved this.

# re: How to access SharePoint's Discussion Board using Web Services 11/8/2007 5:43 PM Szymon Sasin
Hi,

I anm doing similar job but I tried to access a post fro a code. I have real problem with it. It means I can access all Disscussion Board's and all disscusion within them but I could not find a way to read posts itself, could you give me some hints.

# re: How to access SharePoint's Discussion Board using Web Services 4/3/2008 6:53 PM Lukas Vallejo
Hello there! I need to read the top level discussion topics from a discussion list. You say that I can do this using GetListItems method. Can you give me an example that use this method? Tks!

# re: How to access SharePoint's Discussion Board using Web Services 8/21/2008 8:22 PM Sanjay
Here is the sample code to get top level (Question) posted to a discussion list.

strWhere = "<Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>Discussion</Value></Eq></Where>";
SPSiteDataQuery objQuery;
objQuery = new SPSiteDataQuery();
objQuery.Query = strWhereQuery + "<OrderBy><FieldRef Name='DiscussionLastUpdated' Ascending='False' /> </OrderBy>";
objQuery.Lists = " <Lists ServerTemplate='108' /> ";
objQuery.Webs = "<Webs Scope='Web' />";
objQuery.ViewFields = "<FieldRef Name='Author' Nullable='TRUE'/><FieldRef Name='Threading' Nullable='TRUE'/><FieldRef Name='Modified' Nullable='TRUE'/><FieldRef Name='Title' Nullable='TRUE'/><FieldRef Name='ID' Nullable='TRUE'/><FieldRef Name='ContentType' Nullable='TRUE'/><FieldRef Name='DiscussionLastUpdated' Nullable='TRUE'/><FieldRef Name='ItemChildCount' Nullable='TRUE'/><FieldRef Name='Body' Nullable='TRUE'/><FieldRef Name='Editor' Nullable='TRUE'/>";
if (web.GetSiteData(objQuery).Rows.Count > 0)
{
dt = web.GetSiteData(objQuery);
}

# re: How to access SharePoint's Discussion Board using Web Services 9/19/2008 9:05 AM Sarika
How to construct tree view?

# re: How to access SharePoint's Discussion Board using Web Services 4/10/2009 5:48 AM SPGuru
I am so grateful you pointed out the "structure" of discussion forums. Would you be willing to explain how to access the messages using a data view?

# re: How to access SharePoint's Discussion Board using Web Services 8/14/2009 11:41 AM custom dissertation
Would you be willing to explain how to access the messages using a data view?

# re: How to access SharePoint's Discussion Board using Web Services 9/23/2009 8:03 AM college argumentative essays
i think it most useful information any seen

# re: How to access SharePoint's Discussion Board using Web Services 11/24/2009 10:06 AM Carlo
Hi Szymon, have you find a solution to add the replies via web services?
I want to write discussions and related messages to a Sharepoint Online site from a remote PC so I cannot use SPList.
Thank you.


# re: How to access SharePoint's Discussion Board using Web Services 3/24/2010 5:36 AM Vijayalakshmi
(I can post a simple recursive method to construct a tree view from this if anyone needs it).

Can u please post this method. Thanks in advance.

# re: How to access SharePoint's Discussion Board using Web Services 1/24/2011 11:40 AM mensagens para orkut
The code is bit tough.

# re: How to access SharePoint's Discussion Board using Web Services 3/10/2011 10:24 AM Orkut Scraps
In addition to the intranet, AECL other communications vehicles, including the employee newsletter, executive communications including municipalities, the use of email and direct communication media manager to employees. Strengths and weaknesses were noted for each and scored accordingly

# re: How to access SharePoint's Discussion Board using Web Services 10/1/2011 1:26 PM kakadais
hello,
that's very important information to me.
but actually i don't know what is 'discussionUrl' to get the reply message in the folder.
there's No similar inform in the result of getListItems method. (i already checked all field of that method)
it's very pleasure if you can, tell me where can i found that discussionUrl or such a information.
thanks

# re: How to access SharePoint's Discussion Board using Web Services 10/11/2011 11:14 AM shrihari
Can i know wot is the discussionURL.. I am not understanding the discussionurl in the folder node.

# re: How to access SharePoint's Discussion Board using Web Services 11/20/2012 10:56 AM buy essay online
It’s actually a cool and helpful piece of info. I’m happy that you shared this useful information with us. Please keep us up to date like this. Thanks for sharing.


# re: How to access SharePoint's Discussion Board using Web Services 5/12/2013 2:38 PM Cheap Free Running Canada
Awesome data and valuable and great design and style as share excellent things with great suggestions and ideas, a lot of terrific information and inspiration both of those of which I need many thanks of give this sort of a useful information and facts here.


Post A Comment
Title:
Name:
Email:
Comment:
Verification: