/ Published in: JavaScript
![](/img/icon_sav.png?v3)
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>
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>
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<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>
Comments
![RSS Feed for Comments RSS Icon](/images/rss.png?v3)