Friday, 9 March 2012

JavaScript CEWP with Client Context

This gets the last inserted item in table TestJS which has columns ID, Title, URL.


<a href="javascript:TEST()">Click ME!</a><script type="text/javascript">


function TEST()
{
// var myQueryString = '<View/>';

var myQueryString = "<View><Query><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy></Query><RowLimit>1</RowLimit></View>";

var myContext = new SP.ClientContext.get_current();
var myWeb = myContext.get_web();
var myList = myWeb.get_lists().getByTitle('TestJS');
var myQuery = new SP.CamlQuery();
myQuery.set_viewXml(myQueryString);
myItems = myList.getItems(myQuery);

myContext.load(myItems,'Include(ID, Title, URL)');

myContext.executeQueryAsync(Function.createDelegate(this, TestSuccess), null);

}

function TestSuccess(sender,args)
{
var county = myItems.get_count();

var tEnumerator = myItems.getEnumerator();


var tDetails = '';

var currentEnum;
while(tEnumerator.moveNext())
{
currentEnum = tEnumerator.get_current();
tDetails = tDetails + ' ' + currentEnum.get_item('Title')
+ ' ' + currentEnum.get_item('URL');

}


alert("Query Result: " + tDetails);

alert("Last value: " + currentEnum.get_item('URL'));

}</script>


No comments:

Post a Comment