Check If Sharepoint List Already Exists In A Site Using JSOM

Steps
  1. Open your Notepad.
  2. Copy the code given below and paste it.
  3. Name it spvalidation.js.
  4. Add Content Editor Webpart in your page.
  5. Add saved.js file into your Webpart properties.
  1. <script>  
  2. $(document).ready(function()  
  3. {  
  4.   
  5. ChecListalreadyexists('Documents');  
  6. });  
  7. function ChecListalreadyexists(listTitle)  
  8. {  
  9.    var context = SP.ClientContext.get_current();  
  10.    var web = context.get_web();   
  11.    var site = context.get_site();  
  12.    var rootWeb = site.get_rootWeb();  
  13.    var subWebs = rootWeb.get_webs();  
  14.   
  15.    context.load(subWebs, 'Include(Title,Lists)');  
  16.    context.executeQueryAsync(  
  17.     function () {    
  18.          for (var i = 0; i < subWebs.get_count(); i++) {  
  19.             var subWeb = subWebs.itemAt(i);  
  20.             var lists = subWeb.get_lists();  
  21.             if(get_listExists(lists,listTitle)) { //Lists property is initialized   
  22.                console.log(subWeb.get_title());  
  23.             }  
  24.          }  
  25.     },  
  26.     function (sender, args) {  
  27.        console.log(args.get_message());  
  28.     }  
  29.    );     
  30. }  
  31.   
  32.   
  33.   
  34. function get_listExists(lists,listTitle)  
  35. {  
  36.     var listExists = false;  
  37.     var le = lists.getEnumerator();  
  38.     while (le.moveNext()) {  
  39.          var list = le.get_current();  
  40.             if(list.get_title() == listTitle) {  
  41.               listExists = true;  
  42.               break;  
  43.          }  
  44.     }  
  45.     return listExists;  
  46. }  
  47.   
  48. </script>   
Was my blog helpful? If yes, please let me know; and if not, please explain what was confusing or missing.

I’ll use your feedback to double-check the facts, add info and update this blog.