Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!

share|improve this question
    
For the record, "[nodeName='z:row']" doesn't work with jQuery 1.7 –  Christophe Nov 18 '11 at 17:06
    
Everything returned is XML (as you know), so if you did a typeof on your values, you'll see that they are considered "string"'s in JavaScript. You'll have to write functions to convert them to other data types. –  iOnline247 Nov 22 '11 at 20:00
    
I meant determining the Field's datatype on the server, as opposed to the type interpreted by Javascript (which you're correct - will always be a string!) I think I've managed to create a function which can create a usable Javascript object by pulling in the List schema before this, but it's a bit of a pain! –  JP. Nov 23 '11 at 10:17
add comment

1 Answer

up vote 1 down vote accepted

If you use GetList first, you can find out what type each column is.

I've got an internal function toward the bottom of SPServices which I use to display column values which might be helpful for you to look at.

Finally, as for the [nodeName='z:row'] syntax not working with jQuery 1.7, that is correct. In the latest beta of SPServices, I have a fix for this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.