Skip to content

Instantly share code, notes, and snippets.

@garystorey
Last active December 17, 2015 04:28
Show Gist options
  • Select an option

  • Save garystorey/5550321 to your computer and use it in GitHub Desktop.

Select an option

Save garystorey/5550321 to your computer and use it in GitHub Desktop.
Converts Sharepoint ASMX XML to JSON or filter SVC call to needed fields
function filterJSObject (obj) {
//Sharepoint SVC Call - returns JSON
return JSON.parse(JSON.stringify(obj,_filterJsObj));
function _filterJsObj (key, value) {
var badattr ='__type __metadata Attachments ContentType ContentTypeID Created CreatedBy CreatedById Id Modified ModifiedBy ModifiedById Owshiddenversion Path Version'.split(" ");
return (badattr.indexOf(key) > -1) ? undefined : value;
}
}
function xml2JsObject ( xml ) {
var results = {} ,
badattr = 'ows__type ows_Attachments ows_LinkTitle ows_MetaInfo ows__ModerationStatus ows__Level ows_ID ows_UniqueId ows_owshiddenversion ows_FSObjType ows_Created_x0020_Date ows_Created ows_FileLeafRef ows_PermMask ows_Modified ows_FileRef ';
$(xml).filterNode('z:row').each( function( i ) {
var tmp = 0,
thisrow = {},
current,
len = this.attributes.length-1;
while( tmp <= len) {
current = this.attributes[tmp].name;
if ( badattr.indexOf( current ) === -1 ) {
thisrow[current] = this.attributes[tmp].nodeValue;
}
tmp++;
}
results[i] = thisrow;
});
return results;
}
$.fn.filterNode = function (name) { //sharepoint asmx fix for node z:row
return this.find('*').filter(function () {
return this.nodeName === name;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment