There have been a few comments lately on this post I made a while back on how the threaded discussion works.  Their question revolves around working with the SharePoint threaded discussion via web services, and more specifically how to post a response to thread or a response to a response.  I was going to work through that this week in my spare time, but as luck would have it someone internally posted how to do this.  I can’t take credit for it, but was given permission by the author, Srinivas Rao Choudam, to post it to share with the greater community.

As I suspected before getting into this it requires calling two methods in the Lists.asmx web service, AddDiscussionBoardItem and UpdateListItems.  The code for making this happen is shown below.

   1: ListsSoapClient client = new ListsSoapClient("ListsSoap");
   2: client.ClientCredentials.Windows.ClientCredential = (System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
   3: client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
   4:  
   5:  
   6: XmlNamespaceManager manager = null;
   7: XmlElement discussionItem = null;
   8:  
   9: manager = new XmlNamespaceManager(new NameTable());
  10: manager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
  11: manager.AddNamespace("z", "#RowsetSchema");
  12:  
  13: StringBuilder mesBody = new StringBuilder(1024);
  14:  
  15: mesBody.AppendFormat("Message-ID: {0}\n", Guid.NewGuid().ToString());
  16:  
  17: mesBody.AppendFormat("Subject: {0}\n", "Discussion112");
  18:  
  19: mesBody.Append("Mime-Version: 1.0\n");
  20:  
  21: mesBody.Append("Content-type: text/plain; charset=UTF-8\n\n");
  22:  
  23: discussionItem = client.AddDiscussionBoardItem("TeamDiscussion", Encoding.UTF8.GetBytes(mesBody.ToString()));
  24:  
  25: XmlNode discussionNode = discussionItem.SelectSingleNode("rs:data/z:row", manager);
  26:  
  27: string serverUrl = discussionNode.Attributes["ows_ServerUrl"].InnerText;
  28: string fileLeafRef = discussionNode.Attributes["ows_FileLeafRef"].InnerText;
  29:  
  30:  
  31: XmlDocument xmlDocument = new XmlDocument();
  32: XmlElement updatesNode = xmlDocument.CreateElement("Batch");
  33: updatesNode.SetAttribute("RootFolder", serverUrl);
  34: updatesNode.SetAttribute("OnError", "Continue");
  35:  
  36: XmlElement methodNode = xmlDocument.CreateElement("Method");
  37: methodNode.SetAttribute("ID", "0");
  38: methodNode.SetAttribute("Cmd", "New");
  39: updatesNode.AppendChild(methodNode);
  40:  
  41:        
  42: XmlElement bodyNode = xmlDocument.CreateElement("Field");
  43: bodyNode.SetAttribute("Name", "Body");
  44: bodyNode.InnerText = "Body-New";
  45: methodNode.AppendChild(bodyNode);
  46:  
  47: XmlElement contentTypeNode = xmlDocument.CreateElement("Field");
  48: contentTypeNode.SetAttribute("Name", "ContentType");
  49: contentTypeNode.InnerText = "Message";
  50: methodNode.AppendChild(contentTypeNode);
  51:  
  52:  
  53: XmlElement fileLeafRefNode = xmlDocument.CreateElement("Field");
  54: fileLeafRefNode.SetAttribute("Name", "DiscussionTitleLookup");
  55: fileLeafRefNode.InnerText = fileLeafRef;
  56: methodNode.AppendChild(fileLeafRefNode);
  57:  
  58: discussionItem = client.UpdateListItems("TeamDiscussion", updatesNode);