SharePoint Discussion Boards and JavaScript

I recently had to look into exporting the contents of a discussion board on SharePoint 2010. As is sometimes the case, doing so with server-side code was not an option, even though it would have been easier. And, documentation for working with discussion boards from client side code is…thin. I found something that works, sort of.

First:

<script type=”text/javascript”>
$(document).ready(function () {
var soapEnv = “<soapenv:Envelope xmlns:soapenv=’http://schemas.xmlsoap.org/soap/envelope/’><soapenv:Body>&#8221;;
soapEnv += “<GetListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’>&#8221;;
soapEnv += “<listName>Team Discussion</listName>”;

The above is standard for getting ready to call the lists.asmx web service from jQuery.

soapEnv += “<viewFields><ViewFields><FieldRef Name=\”ID\” /><FieldRef Name=\”Title\” /><FieldRef Name=\”Body\” /><FieldRef Name=\”ParentFolderId\” /></ViewFields></viewFields>”;

The above specifies which fields to retrieve. A few things to note:

Id: This is of course, the standard id field that is in any SharePoint list
ParentFolderId: this is the join field. This allows you to connect the reply to the main message. See the following for more on this: http://blogs.msdn.com/b/cliffgreen/archive/2008/07/16/unravel-the-sharepoint-threaded-discussion.aspx
Note, if you need more detail, check out the ThreadIndex field.
Title: Will be populated for the top level items, and blank for the replies

It’s also important to specify the “RecursiveAll” option. Otherwise the call will only return the top level list items, and not the replies:

soapEnv += “<queryOptions><QueryOptions><ViewAttributes Scope=\”RecursiveAll\” /></QueryOptions></queryOptions>”;

I have no explanation for the next piece. If you understand this one, please leave a comment.

soapEnv += “<query><Query><Where><Geq><FieldRef Name=\”ID\” /><Value Type=\”Counter\”>1</Value></Geq></Where></Query></query>”;

Yes, the above states to return all items with an ID of 1 or higher. If the query is left out, it will return the top level items, but not the replies. With this (or any other valid where clause), the replies are returned. (both the query and the RecursiveAll option are needed)

That’s it. Then it’s just a matter of actually making the call and processing the results. Full code sample:

$(document).ready(function () {
var soapEnv = “<soapenv:Envelope xmlns:soapenv=’http://schemas.xmlsoap.org/soap/envelope/’><soapenv:Body>&#8221;;
soapEnv += “<GetListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’>&#8221;;
soapEnv += “<listName>Team Discussion</listName>”;
soapEnv += “<viewFields><ViewFields><FieldRef Name=\”ID\” /><FieldRef Name=\”Title\” /><FieldRef Name=\”Body\” /><FieldRef Name=\”ParentFolderId\” /></ViewFields></viewFields>”;
soapEnv += “<queryOptions><QueryOptions><ViewAttributes Scope=\”RecursiveAll\” /></QueryOptions></queryOptions>”;
soapEnv += “<query><Query><Where><Geq><FieldRef Name=\”ID\” /><Value Type=\”Counter\”>1</Value></Geq></Where></Query></query>”;
soapEnv += “</GetListItems></soapenv:Body></soapenv:Envelope>”;
$.ajax({
url: http://localhost/_vti_bin/lists.asmx&#8221;,
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=\”utf-8\””
});
});

function processResult(xData, status) {
$(xData.responseXML).find(“z\\:row”).each(function () {
alert(“Title: “ + $(this).attr(“ows_Title”) + “\nParentFolderId: “ + $(this).attr(“ows_ParentFolderId”) + ‘\n’ + $(this).attr(“ows_Body”));
});

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s