Check If Field Already Exists In SharePoint 2013 List Using JSOM

Steps
  1. Open your Notepad.
  2. Copy the code given below and paste it.
  3. Name it like 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. var checkFieldExist=checkFieldExists('Documents');  
  6. });  
  7.   
  8. function checkFieldExists(listTitle){  
  9.   
  10.      var context = new SP.ClientContext.get_current();  
  11.      var web = context.get_web();  
  12.      var list = web.get_lists().getByTitle(listTitle);  
  13.      var listFields = list.get_fields();  
  14.      context.load(listFields);  
  15.      context.executeQueryAsync(printFieldNames,onError);  
  16.   
  17.      function printFieldNames() {  
  18.      var e = listFields.getEnumerator();  
  19.            var fieldExist = false;  
  20.               while (e.moveNext()) {  
  21.                    var field = e.get_current();  
  22.                    if (field.get_title() == "myfieldname") {  
  23.                      fieldExist = true;  
  24.                 }  
  25.               }  
  26.               return fieldExist;  
  27.      }  
  28.   
  29.      function onError(sender,args)  
  30.      {   return fieldExist;  
  31.         console.log(args.get_message());  
  32.      }  
  33.   }  
  34. </script>   
Conclusion

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.
Ebook Download
View all
Learn
View all