SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I verify if the list I'm trying to retrieve in Javascript really exists?

My code is:

function Search() {

var context = SP.ClientContext.get_current(); = context.get_site();
this.web = context.get_web();
    Function.createDelegate(this, this.onQuerySucceededSearch),
    Function.createDelegate(this, this.onQueryFailedSearch)

function onQuerySucceededSearch() {
var Cliente = document.getElementById("Ribbon.TabCliente.Ricerca.CercaCliente").value;
var Ufficio = document.getElementById("Ribbon.TabCliente.Ricerca.CercaUfficio").value;
var Commessa = document.getElementById("Ribbon.TabCliente.Ricerca.CercaCommessa").value;

var listaEsiste = web.get_lists().getByTitle(Cliente);

alert(Cliente + Ufficio + Commessa);

if ((Cliente != undefined) && (listaEsiste != undefined)) {
    var doveAndare = "myurl" + Cliente + "/" + Cliente;
    if (Ufficio != undefined) {
        doveAndare += '?&FilterField10=SvDUfficio&FilterValue10=' + Ufficio;
    this.location = doveAndare;
else {
    var doveAndare = "myurl?"
    if (Cliente != undefined) {
        doveAndare += "?&Cliente:" + Cliente;
    if (Ufficio != undefined) {
        doveAndare += "Ufficio:" + Ufficio;
    if (Commessa != undefined) {
        doveAndare += "Commessa:" + Commessa;
    this.location = doveAndare;

 function onQueryFailedSearch(sender, args) {
this.statusID = SP.UI.Status.addStatus("Search:",
    "Fallimento: " + args.get_message() + " <a href='#' onclick='javascript:closeStatus();return false;'>Close</a>.", true);
SP.UI.Status.setStatusPriColor(this.statusID, "red");

myList is always != null, even if the list doesn't exist!

Another question... I have the web object, how could I get the url?

share|improve this question
up vote 2 down vote accepted

Try this code here, and just add the NULL check before moving to the 2nd part of the script

To get the Web Url use properties from the SP.Web, prepended by get_PROPERTY(), in your code use this.web.get_serverRelativeUrl() in the onQuerySucceededSearch.

To check for the list you would need to do this.list = web.get_lists().getByTitle(Cliente) and do again the 2nd context.load under which you would specify a 2nd success handler. To avoid the 2nd loop in your code you would need to move all part where you retrieve the list right after you declare this.web and before context.load(web), that is out of the handler. What happens is that you are making a new method call after the object load is already performed, and you need to "send" this request to the server to be evaluated.

function ViewItem() {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MyList');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, ‘Include(Title,Description)’);
context.executeQueryAsync(Function.createDelegate(this, this.viewSuccess), Function.createDelegate(this, this.failed));
} //handleres omited for brevity
share|improve this answer
So... in my code, where? – Ziba Leah Jul 12 '12 at 12:08

How to check if List exists via CSOM

function get_isListExists(listTitle,OnSuccess,OnError){
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var lists = web.get_lists();

      function() {
        var listExists = false;  
        var le = lists.getEnumerator();
        while (le.moveNext()) {
                var list = le.get_current();
                if(list.get_title() == listTitle) {
                    listExists = true;


share|improve this answer


this.list = web.get_lists().getByTitle(Cliente); 

into the Search function before context.executeQueryAsync

share|improve this answer

Are you executing the context query before then checking the list is undefined, if not it will always be null until you call it.

pseudo code:-

var myList = web.get_lists().getByTitle("ListName");
var myList = web.get_lists().getByTitle("ListName");
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.