Wednesday, February 22, 2012

Query Sharepoint 2010 list via Javascript

Query Sharepoint 2010 list using JavaScript and Client ObjectModel

To read items from a Sharepoint list within a custom page you can use Javascript and client object model, below I explain necessary step to achive this.

Add a new page and reference the Object Model on the content of the page (Sp.js), I add also a reference to the jQuery library for the document ready event.

One very important thing is that you give the page time to load the object model javascript, to do this in the document ready function I call executeOrDelayUntilScriptLoaded that can wait for the fully loaded js.

The code I think is pretty clear: in the document ready I call executeOrDelayUntilScriptLoaded which waits for the loading of object model javascript, and after I calls the function getValues.

Function getValues create the sharepoint object model context and query the specified list for item with ID = 1. To the asynchronous function executeQueryAsync you must specify two callback: the first for the success, and the other for the errors.

If the call return without errors you must get values with the function get_item("ColName").

No comments:

Post a Comment