SharePoint 2013 Create A Site Column Using JSOM

Site columns

A site column is a reusable column definition or a template, which you can assign to the multiple lists, Content Types, across multiple SharePoint sites.

Site columns are useful, if your organization wants to establish some consistent settings across the lists and the libraries. 
  1. $(document).ready(function()  
  2. {  
  3. addField();  
  4. });  
  5.   
  6. function addField() {  
  7. var dfd = $.Deferred();  
  8.    var context = new SP.ClientContext.get_current();  
  9.    var web = context.get_web();  
  10.    var fields = web.get_fields();  
  11.    var fldSchema = '<Field Type="Text" \  
  12.                              Name="TestSiteColumn" \  
  13.                              DisplayName="Test SiteColumn" \  
  14.                              Required="TRUE" \  
  15.                              Group="Gowtham Columns" />';  
  16.    fields.addFieldAsXml(fldSchema);  
  17.    context.executeQueryAsync(Success,Fail);  
  18.    return dfd.promise();  
  19. }  
  20.   
  21. function Success() {  
  22.    alert('SiteColumn created');  
  23.    dfd.resolve();  
  24. }  
  25.   
  26. function Fail()  {  
  27.    alert('site column Creation Failed');  
  28.    dfd.reject();  
  29. }  
  30. </script>   
If you want to create a site column in client-side code, you must use the FieldCollection.AddFieldAsXml method. This method works in exactly the same way as its Server-side equivalent, where you built your field schema as a CAML string and pass it as an argument to the method.

Thanks for reading my blog. Please post your feedback, so that I can improve my future articles and blogs.