Coder Social home page Coder Social logo

jquery-sharepoint-json's Introduction

jQuery SharePoint JSON

This simple jQuery plugin converts XML returned from SharePoint's web services and converts it to JSON.

Features

  • Converts XML elements representing list items into JSON objects according to a user-defined mapping object
  • Designed to be used with Marc D. Anderson's excellent jquery.SPServices library

Usage

$(xml).sharePointJSON(options);

Where 'xml' is XML document returned from a SP web service call to 'GetListItems' (i.e. xdata.responseXML()), and 'options' (optional) is an object containing the following options (both likewise optional):

Options

  • mapping: A mapping object of {jsonAttrs: "ows_SharePointColumns"}. This object defines a mapping between SharePoint columns and JavaScript object keys. Each SharePoint list item will be parsed into a JavaScript object with the jsonAttrs as keys whose values are those of the corresponding SharePoint columns. The SP column names can be identified by inspecting the AJAX return from the web service call to 'GetListItems'. The mapping can also contain a special entry pointing to the source list:

    If this option is not given, only 'title', 'created' and 'modified' will be parsed out, using the default column names from a SP list.

    This function is smart enough about SP XML to parse out arrays, lookup values, etc.

  • callback: A function to be called for each object parsed from the xml. This function should take a single argument, the object parsed from the XML, and its return value will be added to the array to be returned from the main sharePointJSON function. It is recommended that the callback return the object itself, modified in some way.

Returns

It returns an array of objects corresponding to the list items.

Example

Here's the most basic example (this will only parse out 'title', 'created', and 'modified'.):

$.post("http://sharepointsite.example.com/_vti_bin/Lists.asmx", soapRequestXML, function(data){
  var json = $(data).sharePointJSON();
  // Do some stuff...
});

Here's an example with a mapping object:

var mapping = {foo: "ows_Foo", barBaz: "ows_Bar_x0020_Baz"};

$.post("http://sharepointsite.example.com/_vti_bin/Lists.asmx", soapRequestXML, function(data){
  var json = $(data).sharePointJSON({mapping: mapping});
  
  $.isArray(json) // True
  $.each(function(i, obj){
    console.log("foo: %o", obj.foo);
    console.log("barBaz: %o", obj.barBaz)
  });
});

And here's an example of using an object callback:

var callback = function(obj){obj.foo = "<b>" + obj.foo + "</b>"; return obj;};
var mapping = {foo: "ows_Foo", barBaz: "ows_Bar_x0020_Baz"};

$.post("http://sharepointsite.example.com/_vti_bin/Lists.asmx", soapRequestXML, function(data){
  var json = $(data).sharePointJSON({mapping: mapping, callback: callback});
  
  $.isArray(json) // True
  $.each(function(i, obj){
    console.log("foo: %o", obj.foo); // prints "foo: <b>[value of "foo"]</b>"
    console.log("barBaz: %o", obj.barBaz)
  });
});

jquery-sharepoint-json's People

Contributors

bobby avatar lukeholder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.