Add a Field (Column) to a List in SharePoint 2013 Using JavaScript

This article shows how to write code to do basic operations using the JavaScript client object model in SharePoint 2013.

You can use the SharePoint client object model to retrieve, update and manage data in SharePoint 2013. SharePoint makes the object model available in several forms.

  • .NET Framework redistributable assemblies.
  • JavaScript library.
  • REST/OData endpoints.
  • Windows Phone assemblies.
  • Silverlight redistributable assemblies.

This article shows how to do basic operations using the JavaScript object model. You can add a reference to the object model using HTML <script> tags.

The following sections describe tasks that you can complete programmatically and they include JavaScript code examples that show the operations.

Procedure

Open your SP Site in SharePoint 2013 Designer. Then select an Assets icon from the designer ribbon to add a JavaScript file.



After adding a JavaScript file, the file is available in the SharePoint Site Assets Folder.



Go to the SP site page and add a new page to the SharePoint site.



After adding a page select an Insert button in the Ribbon Menu.



Then add a Content Editor Web part into the page.



Edit the WebPart and add a JavaScript file link to the contents link.



Save the web part and save the page in the site. Click the button.



List Created Successfully.

Source Code

  1. <div>  
  2.     <button onclick=" addFieldToList()">Click here to AddaFieldtoList</button>  
  3. </div>  
  4. <div id="displayDiv"></div>  
  5.    <script type="text/javascript">   
  6.   
  7.    //This function loads the list and runs the query asynchronously   
  8.   
  9.    function addFieldToList() {  
  10.   
  11.       var siteUrl="https://gauti.sharepoint.com/sites/sp/";  
  12.       var clientContext = new SP.ClientContext(siteUrl);  
  13.   
  14.       var oList = clientContext.get_web().get_lists().getByTitle('Announcements');  
  15.       this.oField = oList.get_fields().addFieldAsXml(  
  16.       '  
  17.     <Field DisplayName=\'MyField\' Type=\'Number\' />',   
  18.       true,   
  19.       SP.AddFieldOptions.defaultValue  
  20.    );  
  21.   
  22.    var fieldNumber = clientContext.castTo(oField,SP.FieldNumber);  
  23.    fieldNumber.set_maximumValue(100);  
  24.    fieldNumber.set_minimumValue(35);  
  25.    fieldNumber.update();  
  26.   
  27.    clientContext.load(oField);  
  28.    clientContext.executeQueryAsync(  
  29.    Function.createDelegate(this, this.onQuerySucceeded),   
  30.    Function.createDelegate(this, this.onQueryFailed)  
  31.    );  
  32. }  
  33.   
  34. function onQuerySucceeded() {  
  35.    var result = oField.get_title() + ' added.';  
  36.    alert(result);  
  37. }  
  38.   
  39. function onQueryFailed(sender, args) {  
  40.    alert('Request failed. ' + args.get_message() +   
  41.    '\n' + args.get_stackTrace());  
  42. }  
  43.   
  44. </script>  
Thanks for reading my article.