Create Site Column In SharePoint From OOB And JSOM

In this article, I would like to share the steps to create a site column and code to create the site column in SharePoint using JSOM. Most of us know how to create a column in a SharePoint list. If we create a field as a site column, we can reuse the column in multiple lists when we need the same data type and column.

SharePoint Columns

  • SharePoint list is used to categorize and group the columns and track the information in lists and libraries
  • Multiple data types can be used in a column, such as a single line of text, multi-line of text, person or group, number, dropdown list etc.

Steps to create a site column 

Follow the below-listed steps to create a site column.

Step 1

Open the SharePoint site on your browser.

SharePoint

Step 2

Then, go to the "Site Settings" page by clicking the gear icon on the site.

SharePoint

 Step 3

On the Site Settings page, you can see site columns under the “Web Designer galleries” category as shown below.

SharePoint

Step 4

Then, we can create the column in site columns page.

SharePoint

 

Step 5

Click “Create” button to create a new site column as shown below.

SharePoint

 

Step 6

On "Create Column" page, provide field name and select the type in “Name and Type” category, as shown below.

SharePoint

Step 7

Then, select the group and additional column settings and click “OK” button to create a new field.

SharePoint

SharePoint

Finally, you can see the site field in Custom column group.

SharePoint

Here is the code to create site column using JavaScript Object Model.

  1. $(document).ready(function()    
  2. {    
  3. createSiteField();    
  4. });    
  5.     
  6. function createSiteField() {     
  7.    var context = new SP.ClientContext.get_current();    
  8.    var web = context.get_web();    
  9.    var fields = web.get_fields();    
  10.    var fldSchema = '<Field Type="Text" \    
  11.                              Name="sampleColumn" \    
  12.                              DisplayName="Test SiteColumn" \    
  13.                              Required="TRUE" \    
  14.                              Group="Gowtham Columns" />';    
  15.    fields.addFieldAsXml(fldSchema);    
  16.    context.executeQueryAsync(Success,Fail);    
  17.      
  18. }    
  19.     
  20. function Success() {    
  21.    alert('SiteColumn is created successfully');    
  22.     
  23. }    
  24.     
  25. function Fail()  {    
  26.    alert('site column Creation Failed');    
  27.     
  28. }  

Summary

In this article, we have explored how to create a site column in SharePoint using UI and using JavaScript object model.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now