Return to Snippet

Revision: 29177
at June 28, 2011 11:52 by rumremix


Updated Code
<script type="text/javascript">   

ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js");   //this is necessary to ensure the library is loaded before function triggered
 var namedListItem;  


function functionName() {


var clientContext = SP.ClientContext.get_current();
//or use the syntax below for accessing a list that may not reside in the current site
//var clientContext = new SP.ClientContext(rootUrl);

var myList = clientContext.get_web().get_lists().getByTitle('myList'); //actual list name here

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(''); //caml statement goes here between the single quotes
namedListItem = myList.getItems(camlQuery);
        
   clientContext.load(namedListItem);
   //or use below to specify to load only the required properties for better performance
   //clientContext.load(namedListItem, 'Include(field1, field2)');
        
   clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = namedListItem.getEnumerator();
        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo +=  '\nField1: ' + oListItem.get_item('field1') +    ', Field2: ' + oListItem.get_item(' field2'); //replace field1 & field2 with actual column names
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

Revision: 29176
at June 24, 2011 11:14 by rumremix


Updated Code
<script type="text/javascript">   

ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js");   //this is necessary to ensure the 
library is loaded before function triggered
 var namedListItem;  


function functionName() {


var clientContext = SP.ClientContext.get_current();
//or use the syntax below for accessing a list that may not reside in the current site
//var clientContext = new SP.ClientContext(rootUrl);

var myList = clientContext.get_web().get_lists().getByTitle('myList'); //actual list name here

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(''); //caml statement goes here between the single quotes
namedListItem = myList.getItems(camlQuery);
        
   clientContext.load(namedListItem);
   //or use below to specify to load only the required properties for better performance
   //clientContext.load(namedListItem, 'Include(field1, field2)');
        
   clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = namedListItem.getEnumerator();
        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo +=  '\nField1: ' + oListItem.get_item('field1') +    ', Field2: ' + oListItem.get_item(' field2'); //replace field1 & field2 with actual column names
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

Revision: 29175
at May 17, 2011 07:42 by rumremix


Updated Code
<script type="text/javascript">   

ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js");   //this is necessary to ensure the library is loaded before function triggered 


function functionName() {


var clientContext = SP.ClientContext.get_current();
//or use the syntax below for accessing a list that may not reside in the current site
//var clientContext = new SP.ClientContext(rootUrl);

var myList = clientContext.get_web().get_lists().getByTitle('myList'); //actual list name here

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(''); //caml statement goes here between the single quotes
this.collListItem = myList.getItems(camlQuery);
        
   clientContext.load(collListItem);
   //or use below to specify to load only the required properties for better performance
   //clientContext.load(collListItem, 'Include(field1, field2)');
        
   clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();
        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo +=  '\nField1: ' + oListItem.get_item('field1') +    ', Field2: ' + oListItem.get_item(' field2'); //replace field1 & field2 with actual column names
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

Revision: 29174
at July 23, 2010 07:56 by rumremix


Initial Code
<script type="text/javascript">   

ExecuteOrDelayUntilScriptLoaded(functionName, "sp.js");   //this is necessary to ensure the library is loaded before function triggered 


function functionName() {


var clientContext = SP.ClientContext.get_current();
var myList = clientContext.get_web().get_lists().getByTitle('myList'); //actual list name here

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(''); //caml statement goes here between the single quotes
this.collListItem = myList.getItems(camlQuery);
        
   clientContext.load(collListItem);
   //or use below to specify to load only the required properties for better performance
   //clientContext.load(collListItem, 'Include(field1, field2)');
        
   clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';

    var listItemEnumerator = collListItem.getEnumerator();
        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo +=  '\nField1: ' + oListItem.get_item('field1') +    ', Field2: ' + oListItem.get_item(' field2'); //replace field1 & field2 with actual column names
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

Initial URL

                                

Initial Description
This code works for me without requiring any includes, plugins, or service packs. It should also integrate seamlessly with jquery. If needing to perform this process more than once on a page, make sure namedListItem variable is unique for each case (use different names for that page-scoped variable in each case).

For caml queries, wrap Query tag in View tag.

For lookup fields use .get_lookupValue() property syntax. For example...

<pre>topNavSection[enm] = oListItem.get_item('Section').get_lookupValue();</pre>

Initial Title
SHAREPOINT 2010 READ FROM LIST USING ECMASCRIPT

Initial Tags
javascript, list, object, sharepoint

Initial Language
JavaScript