Sunday, 25 August 2013

SharePoint List Query JavaScript Variable Access Issue

SharePoint List Query JavaScript Variable Access Issue

I am trying to figure out why I cannot access the variable theHeaders
outside of the function. I have tried returning it and then calling the
function directly and I get a colListItem on defined. I called it like so:
`alert(onQuerySucceeded());` and `alert(onQuerySucceeded(sender, args));`
I get an undefined everytime.
How can I access theHeaders outside of the functions?
JavaScript:
var siteUrl = '/sites/dev/';
ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
function retrieveListItems()
{
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('myList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<Where><IsNotNull><FieldRef Name='Title'
/></IsNotNull> </Where>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,
this.onQuerySucceeded), Function.createDelegate(this,
this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{
var listItemEnumerator = collListItem.getEnumerator();
var theHeaders = "";
while (listItemEnumerator.moveNext())
{
var oListItem = listItemEnumerator.get_current();
theHeaders = theHeaders + oListItem.get_item('Title');
}
alert(theHeaders);
}
function onQueryFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + '\n' +
args.get_stackTrace());
}

No comments:

Post a Comment