SPXmlToJson - calculated fields and "blank" xml attributes

May 17, 2012 at 2:34 AM

Hi,

Started using this library the other day and I've been using SPXmlToJson to return data from SP lists to display in the Kendo UI grid control - it works great.

I wanted to share a couple of changes/additions I made to the function to get round the missing XML attributes when there's no value and also allow it work handle calculated fields.

I apologize if this has been done already, I couldn't find anything in the discussions.

Under the assumption that if you mapped a column, you want to see that column regardless of if it has data in it or not, I added:

$.each(opt.mapping, function() {
  row[this.mappedName] = '';
});

 

and a supporting function for attrToJson for it to handle calculated columns:

function calcToJsonObject(s) {
  if(s.length === 0) {
    return null;
  } else {
    var thisUser = s.split(";#");
    return attrToJson(thisUser[1], thisUser[0]);
  }
}

 

There are probably other types returned by calculated fields, but that shouldn't be a big problem.

Hope someone find's this useful!

 

 

Scott.

 

 

Full:

// This function converts an XML node set to JSON
// Initial implementation focuses only on GetListItems
$.fn.SPXmlToJson = function(options) {

	var opt = $.extend({}, {
		mapping: {},			// columnName: mappedName: "mappedName", objectType: "objectType"
		includeAllAttrs: false, // If true, return all attributes, regardless whether they are in the mapping
		removeOws: true			// Specifically for GetListItems, if true, the leading ows_ will be stripped off the field name
	}, options);

	var colNum;
	var jsonObject = [];

	this.each(function() {
		var row = {};
		var colValue;
		var rowAttrs = this.attributes;

// bring back all mapped columns, even those with no value
		$.each(opt.mapping, function() { 
			row[this.mappedName] = ''; 
		});
		
		for(attrNum = 0; attrNum < rowAttrs.length; attrNum++) {
			var thisAttrName = rowAttrs[attrNum].name;
			var thisMapping = opt.mapping[thisAttrName];
			var thisObjectName = thisMapping !== undefined ? thisMapping.mappedName : opt.removeOws ? thisAttrName.split("ows_")[1] : thisAttrName;
			var thisObjectType = thisMapping !== undefined ? thisMapping.objectType : undefined;
			if(opt.includeAllAttrs || thisMapping !== undefined) {
				row[thisObjectName] = attrToJson(rowAttrs[attrNum].value, thisObjectType)
			}
		}
		// Push this item into the JSON Object			
		jsonObject.push(row);

	});

	// Return the JSON object
	return jsonObject;

}; // End $.fn.SPServices.SPXmlToJson


function attrToJson(v, objectType) {

	switch (objectType) {
		case "DateTime":
			// Dates have dashes instead of slashes: ows_Created="2009-08-25 14:24:48"
			colValue = dateToJsonObject(v);
			break;
		case "User":
			colValue = userToJsonObject(v);
			break;
		case "UserMulti":
			colValue = userMultiToJsonObject(v);
			break;
		case "Lookup":
			colValue = lookupToJsonObject(v);
			break;
		case "LookupMulti":
			colValue = lookupMultiToJsonObject(v);
			break;
		case "Boolean":
			colValue = booleanToJsonObject(v);
			break;
		case "Integer":
			colValue = intToJsonObject(v);
			break;
		case "Counter":
			colValue = intToJsonObject(v);
			break;
		case "MultiChoice":
			colValue = choiceMultiToJsonObject(v);
			break;
		case "Currency", "float":
			colValue = floatToJsonObject(v);
			break;
		case "Calc":
			colValue = calcToJsonObject(v);
			break;
		default:
			// All other objectTypes will be simple strings
			colValue = stringToJsonObject(v);
			break;
	}
	return colValue;
}

function calcToJsonObject(s) {
	if(s.length === 0) {
		return null;
	} else {
		var thisUser = s.split(";#");
		return attrToJson(thisUser[1], thisUser[0]);
	}
}
. . . . .

Coordinator
May 22, 2012 at 3:04 AM

Thanks for the submission. I'll take a look and try to get it into a future release.

M.

Coordinator
May 22, 2012 at 3:04 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Jun 16, 2012 at 2:52 AM

I just added your changes to v0.7.2ALPHA4. Give 'em a test and please let me know if you see any problems.

Thanks for the submission!

M.