I'm using SPServices with jQuery to collect data from a Sharepoint 2010 list for use in a standalone Javascript powered web application.
EDIT: I've found a little more out while investigating this - the big question now is how can I determine the datatype (eg. Integer, Float, Person, Choice, Lookup etc) of each field from the SOAP response?
I can collect all the data fine, but the data returned isn't very nice to work with. Field names begin with ows_ and have spaces replaced by 0x0020 and other such encodings. Fields representing unusual datatypes (people, lookup fields and so on) have what I assume are IDs and hashes and semicolons seperating them. Arrays seem to be delimited by #; too.
I've written some code to create a decent javascript object from the XML returned, but I'd like some feedback in case there are some use cases where it'll break and I really need to be able to get the user's full account name (with domain) rather than their name when a person field is returned. Any ideas?
function parse_SP(xData) {
var array = new Array();
$(xData.responseXML).find("[nodeName='z:row']").each(function() {
var element = new Array();
$.each(this.attributes,function(i,attrib) {
var name = attrib.name.replace(/x0020/ig,' ')
var val = attrib.value
/* I need to collect DOMAIN\USERNAME
* from what's returned for people fields,
* maybe this needs to be done with a specific
* CAML query?
**/
var match = name.match(/^ows_(?!_)(.+?)(?:\ \((.+)\))?(0)?$/)
if (match != null) {
name = match[1].replace(/_/g,'')
if (match[3] == '0') {
val = parseFloat(val)
} else {
if (name != 'UniqueId') {
var special = val.split(';#');
if (special.length > 1) {
switch (special[0]) {
case '':
// Is an array
special.pop();
special.shift();
val = special.join(', ');
break;
}
}
}
}
element[name] = val;
}
})
array.push(element);
})
return array;
}
This function is used like this:
$().SPServices({
operation: "GetListItems",
async: false,
// Next line added, as per edit below, to return expanded user fields
CAMLQueryOptions:'<QueryOptions><ExpandUserField>TRUE</ExpandUserField></QueryOptions>',
listName: 'My Sharepoint List',
completefunc: function (xData, Status) {
$.each(parse_SP(xData),function(i,p) {
console.log(p);
})
}
})
EDIT
Turns out you can collect the full details of a 'person' field by adding CAMLQueryOptions:'<QueryOptions><ExpandUserField>TRUE</ExpandUserField></QueryOptions>',
to the SPServices call (as included above) - the only issue I have now (and the overriding issue from this whole question) is: how can you determine a field's data type from the SOAP data sent back?
Thanks for your time!