2

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();
this.site = context.get_site();
this.web = context.get_web();
context.load(this.site);
context.load(this.web);
context.executeQueryAsync(
    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?

4 Answers 4

2

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

http://www.learningsharepoint.com/2011/05/16/get-all-list-items-using-ecmascript-sharepoint-2010/

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
1
  • So... in my code, where? Commented Jul 12, 2012 at 12:08
3

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();
    ctx.load(lists); 

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

        OnSuccess(listExists);
      },
      OnError
    );
}


get_isListExists('Documents',function(listExists){
    console.log(listExists);
},function(sender,args){
    console.log(args.get_message());
});
1

Put

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

into the Search function before context.executeQueryAsync

0

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");
clientContext.ExecuteQuery();
var myList = web.get_lists().getByTitle("ListName");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.