Tag: Web Services

jQuery Random Quote Viewer

While doing some housekeeping recently on my personal site I was tinkering around with a pre-jQuery ajax enabled web part I had written to display random quotes from a SharePoint list on the site. The code was pretty ugly so I decided to adapt the web service call to use jQuery instead in line with the similar functions displays I’ve been deploying lately.

I thought I might take the time to share it as well since jQuery is still a hot topic.

Here is what the display looks like.jQuery Random Quote Viewer

The SharePoint list is a basic list with a column for the Quote and a column for the Author.

The code is currently hosted in a Content Editor Web Part (CEWP) and is pretty simple stuff. If you are going to use this, just be sure to put in the name or GUID of the list and the URL to the site’s list web service. Here is what it looks like:



$(document).ready(function() {
var URL = ""; // URL ex. http://mycompany/sites/sub-site/_vti_bin/lists.asmx
var listName = ""; // List Name or GUID

var soapEnv = "


" + listName + "





";

$.ajax({
url: URL,
beforeSend: function(xhr) {
xhr.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/GetListItems");
},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: DisplayQuote,
error: function(xhr) { $("#errorContainer").html('Error: ' + xhr.status + ' ' + xhr.statusText); },
contentType: "text/xml; charset="utf-8""
});
});

function DisplayQuote(xData, status)
{
var Output;
var WikiString = "http://en.wikipedia.org/wiki/Special:Search?fulltext=Search&search;="
var xmlDoc = xData.responseXML;
var ListItems = xmlDoc.getElementsByTagName("z:row") || xmlDoc.getElementsByTagNameNS("*","row");
var SelectedItem = Math.floor(Math.random()* ListItems.length);
var Quote = ListItems[SelectedItem].getAttribute("ows_Quote");
var Author = ListItems[SelectedItem].getAttribute("ows_Author0");
Output = "" + Quote + " - " + "" + Author + "";
$("#quoteContainer").html(Output);
}

While this is simple code, it can be applied to so many different things.  You can use it to generate a slide show, show random images, etc. 

The source files are available here:

 

Related Links

Exposing Custom Web Services to the BDC

I recently had a lot of trouble getting some data exposed to the BDC.  I found the documentation pretty light and leading to a pretty frustrating experience.

In my case there are three scenarios:

Return All Records, No Finder – This one is pretty easy and it is clear that you need to return a collection of records. 

Return One Record, Specific Finder – This one is pretty simple as well and it is clear that you will get back a single record.

Return One Record, Finder – This one was the cause of all my trouble.  It seemed to me that if you are only expecting a single record then it could be defined as such.  Using BDC MetaMan I was able to generate a definition for all three types, and then able to import the definition into the BDC. 

Whenever I tried to use show the data BDC List web part I kept seeing an error.  In the ULS logs I found the following:

System.InvalidOperationException: Backend system adapter returned a structure incompatible with the corresponding metadata (MethodInstance, Parameter or TypeDescriptor)    

I of course concentrated on checking data types and matched up the web service to the definition file to make sure things were in line.  They matched perfectly, so this proved to be quite the hurdle.  Finally on MSDN I caught a reference to the error “a structure incompatible with the corresponding metadata” and making sure the Collection was specified correctly.  Even this reference did not specify that a collection was required.  We made a quick change to the web service’s returned data type and everything started working. 

So the moral of the story is… when using the BDC with anything but a Specific Finder it requires a Collection/Array to be returned.

%d bloggers like this: