Check File Already Exists In Document Libruary Using Sharepoint 2013 List Using JSOM

SharePoint 2013 supports API methods given below to perform basic operations like Create, Update, Read and Delete.
  • Server Side Object Model (SSOM).
  • Client Object Model (CSOM).
  • JavaScript Object Model (JSOM).
  • REST.
Steps
  1. Open your Notepad.
  2. Copy the code given below and paste it.
  3. Name it as spvalidation.js.
  4. Add Content Editor Webpart in your page.
  5. Add saved .js file into your Webpart properties.
Code
  1. <script>  
  2. $(document).ready(function()  
  3. {  
  4.   
  5. var checkFieldExist=checkFieldExists();  
  6. });  
  7.   
  8. function checkFieldExists(){  
  9.   
  10.             var ctx = SP.ClientContext.get_current();  
  11.                
  12.             var folder = ctx.get_web().getFolderByServerRelativeUrl("/path/to/folder");  
  13.                
  14.             ctx.load(folder, "Exists""Name");  
  15.                
  16.             ctx.executeQueryAsync(  
  17.                function() {  
  18.                   if (folder.get_exists()) {  
  19.                        
  20.                      console.log(folder.get_name());  
  21.                   }  
  22.                   else {  
  23.                      console.log("Folder exists but is hidden (security-trimmed) for us.");  
  24.                   }  
  25.                },  
  26.                function(s, args) {  
  27.                   if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {  
  28.                       
  29.                      console.log("Folder does not exist.");  
  30.                   }  
  31.                   else {  
  32.                       
  33.                      console.log("Error: " + args.get_message());  
  34.                   }  
  35.                }  
  36.             );  
  37.   
  38. }  
  39. </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.