SharePoint site columns | Create site column in SharePoint using Rest API and PowerShell

This SharePoint tutorial explains, what is site column in SharePoint? How to create a site column in SharePoint Online or SharePoint 2013/2016? We will also discuss how to modify a site column in SharePoint? Let us discuss on SharePoint site columns and how to create a site column using Rest API in SharePoint Online or SharePoint 2013, SharePoint 2016, or SharePoint 2019.

Let us learn:

  • How to add a site column to list or library in SharePoint Online

  • sharepoint site columns vs list columns

  • sharepoint site columns best practices

  • create site column in sharepoint online using PowerShell

How to create a SharePoint site column? To create a site column in SharePoint, Open the SharePoint Online site, Go to the Site settings page, then under Web Designer Galleries click on Site columns. Then click on Create, then provide the column name, choose the data type, and select the Group. Then click on Create.

Let us check out in details.

What are SharePont site columns

What is s SharePoint site column? A SharePoint site column is a reusable column that we can use in any list and library in the site and any nested subsite under the SharePoint site. By creating a site column at the site level we can use that in any lists and libraries of the current site and child sites in SharePoint.

For example suppose we create a list which contains “department name” column and we want to add the “department name” column in another list, which cannot be possible. So instead of creating the column in the list level, we have to create the column in the site level as a site column.

Create a Site Column in SharePoint

Follow the below steps to create a site column in SharePoint 2016/2013 or SharePoint Online.

Step-1: Open SharePoint site, from gear icon click on “Site settings”.

site column sharepoint

Step-2: In Site Settings page click on “Site columns“.


site column sharepoint 2013

Step-3: In the site columns page click on “Create“.

sharepoint 2013 site columns

Step-4: In create column page we have to give a column name here I am giving “Department Name” as column name. From the “type of information in this column is” we can choose any options but I have chosen the “choice”.

site column sharepoint online Step-5: In the group, we can choose the existing group or new group but here I have been chosen “New group” and give the name as “Department Group” and the description field is there which is nothing but the description of our site column. One more option we can see “Type each choice on a separate line”, here we can add the items. I am giving “HR, Finance, IT, Accountant “items in that field.

sharepoint site columns vs list columns Step-6: We can see “Display choice using” field we can choose anyone. I have chosen the dropdown so in list we can see out items in dropdown. In “Allow ‘Fill-in’ choices” field we can see “NO” option is selected which is by default. Below that field “Default Value” field is there here I am selecting the choice option and type HR. So when we open the column in list we can see “HR” item in the above. Then click on “OK”.

sharepoint 2013 site columns

Step-7: We can see out site column group in “All group”.

how to create site columns in sharepoint 2016

Add SharePoint Site Column to List or Document Library

We have created the site column, now we will see how we can add the site column to a SharePoint 2016/2013 or SharePoint Online list.

Step-1: Here I am creating the custom list named “Employee List”. In Employee, list page, go to “list” which is present in the ribbon. Click on list settings in the list option to open the list settings page.

Step-2: In list setting page, go to the columns section and you can find “Add from existing site columns”, click on that.

how to create site columns in sharepoint 2013

Step-3: Now we can find the page like below. Here in “Select the site column from” field we have to chose our site column group then click on “Add”.

how to create site columns in sharepoint online

Step-4: Here we can see our site column is added then click on ok.

sharepoint 2016 site column

Step-5: In the Employee list page we can found our site column (the highlighted one).

What is SharePoint site column?

Step-6: When we click on a new item we can see the page like below. In “Employee Name” field I am giving the name and in department name we can see the dropdown. Click on Save.

sharepoint site column

Step-7: In Employee list we can see our department name and Employee name.

sharepoint site columns best practices

Edit a site column in SharePoint

The following steps can be followed to modify the site columns

Step-1: Go to site column page, click on “All group”. We can see our “Department Group” which will show all the site column.

SharePoint 2013 site column

Step-2: We have click on the column name which we want to edit. Now we can see edit column page where we can edit. I am adding a new “Department name” as “Management” in my site column like below then click on “OK”.

SharePoint online site column

Step-3: When we open the list page and click on “Add new items” we can found the following dropdown, click on “Save”.

Sharepoint 2016 site column

Step-4: Now we can see our list page like below.

site column SharePoint 2016

Create SharePoint site column programmatically using Rest API

Now, let us see how to create a SharePoint site column programmatically using SharePoint Rest API. The same Rest API code you can use to create SharePoint 2013 site columns.

Here, we will provide a user the option to enter the site column name and there will be a button, on click of the button, the SharePoint site column will get created.

The below code you can add inside a SharePoint script editor web part or content editor web part.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>  
<script> 
$(function(){ 
$("#btnClick").click(function()
{ 
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/fields"; 
var siteColumnname = $("#txtSiteColumnName").val(); 
$.ajax({ 
url: requestUri, 
type: "POST", 
data: JSON.stringify( 
{ '__metadata': { 
'type': 'SP.Field' }, 
'Title': siteColumnname, 
'FieldTypeKind': 2, 
'Group': 'SPGuides Site Columns' 
}), 
headers: { 
"accept": "application/json;odata=verbose", 
"content-type": "application/json;odata=verbose", 
"X-RequestDigest": $("#__REQUESTDIGEST").val() 
}, 
success: onSuccess, 
error: onError  

});  

function onSuccess(data) { 
alert('Site column created successfully'); 
}  

function onError(error) { 
alert(JSON.stringify(error)); 
} 
});  
}); 
</script>  
<table> 
<tr> 
<td> Enter Site Column Name: <input type="text" id="txtSiteColumnName"> 
</td> 
<td> 
<input type="button" id="btnClick" value="Create Site column using REST API"/> 
</td> 
</tr> 
</table>

Once you Save the code, the HTML form will look like below:

Create SharePoint site column programmatically using Rest API

Once you provide the SharePoint site column name and click on the button, the SharePoint site column will be created.

This is how we can create site columns programmatically using Rest API in SharePoint. The rest api code, you can use to create SharePoint 2013 site columns programmatically using Rest API.

SharePoint site column vs list column

Should I create site columns or list columns in SharePoint? Trying to find out the difference between SharePoint site column and list column? Read to know more on the SharePoint site column vs list column. There are two types of columns we can create in SharePoint to store metadata or information.

  • List columns

  • Site columns

If you are directly creating columns in a SharePoint list, those are known as the list columns and those are available only in that specific list. You can not reuse list columns in any other list or site.

On the other hand, we can reuse the SharePoint site columns in a site and the sites and nest subsites. So basically site columns are reusable in parent sites as well as all the subsites.

You can check blow where the SharePoint site columns will be available.

Apart from that, when you create a site column, SharePoint automatically index those columns behind the scene.

SharePoint site columns best practices

Now, let us see some of the best practices you should follow while creating site columns in SharePoint.

  • Like columns, always provide short, meaningful names for your site column. Do not provide space or special characters for your SharePoint column name. If you will provide space, the site column will render as %20 for space. Like the First Name will appear like First%20Name as the internal name.

  • When you are created site columns provide the internal names properly, maybe append with your company name or project name. Like TSInfoFirstName, TSInfoLastName will be the internal names and the display names will be First Name and Last Name. There are lots of predefined site columns are available in SharePoint and if you will try to create a custom site column with the same name, it will not allow. For example, instead of the Department, you can provide the internal name as TSInfoDepartment.

  • For all your SharePoint custom site columns, create a group to categories all your site columns. You can use your company name or project name as the group like TSInfo Site Columns.

  • If you want to create more site columns, then do not create manually, always you can create a CSV or excel file to keep the information like column display name, internal names, default values, etc. And then you can write a piece of code to create the SharePoint site columns programmatically using PowerShell, Rest API, CSOM, etc.


Create site column in SharePoint Online using PowerShell

Now, let us see how to create a site column in SharePoint Online using PowerShell.

Below is the PowerShell script you can use to create SharePoint site column using PowerShell.

You can run the below PowerShell script using Windows PowerShell ISE.

Single Line of Text:

Below is the PowerShell script to create a SharePoint site column of type Single line text. Here, we need to add Type=’Text’ for the Field.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='SPGuides Site Columns' $internalName='SPGuidesYourFirstName' 
$displayName='Your First Name'  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $credentials= Get-Credential 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, $credentials.Password)  

$allColumns = $ctx.web.Fields 
$ctx.Load($allColumns) 
$ctx.executeQuery() 
$newColumn = $allColumns | where {$_.Title -eq $displayName} if($newColumn -ne $NULL)      
    {         
        Write-host "SharePoint Site Column already exists!"     
    }     
    else     
    {                 
        $FieldSchema = "<Field Type='Text' DisplayName='$displayName' Name='$internalName' required='False' Group='$siteColumnGroupname'/>"         
        $newColumn = $allColumns.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)                     
        $ctx.ExecuteQuery()              
        
        Write-host "SharePoint Site Column Created Successfully!"     
    }

Once you run the above PowerShell script, the SharePoint site column will be created successfully like below:

Create site column in SharePoint online using PowerShell

This is how we can create site column in SharePoint online using PowerShell.

Multiple lines of text:

Below is the PowerShell script to create multiple lines of text SharePoint site column. In the Type, set as Note in the Field like Type=’Note’.


Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='SPGuides Site Columns' $internalName='SPGuidesYourAddress' $displayName='Your Address'  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

$credentials= Get-Credential 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, 
$credentials.Password)  

$allColumns = $ctx.web.Fields 
$ctx.Load($allColumns) $ctx.executeQuery() 
$newColumn = $allColumns | where {$_.Title -eq $displayName} if($newColumn -ne $NULL)      
    {         
        Write-host "SharePoint Site Column already exists!"     
    }     
    else     
    {                 
        $FieldSchema = "<Field Type='Note' DisplayName='$displayName' Name='$internalName' required='False' Group='$siteColumnGroupname'/>"         
        $newColumn = $allColumns.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)                 
        $ctx.ExecuteQuery()  
            
        Write-host "SharePoint Site Column Created Successfully!"     
    }

This is how we can create a multiline text column in SharePoint Online using PowerShell.

Date and Time:

Below is the PowerShell script to create site column of type Date and Time. You can add additional parameters like Format=’DateOnly’.


Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='SPGuides Site Columns' $internalName='SPGuidesJoininDate' $displayName='Your Joining Date'  $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

$credentials= Get-Credential 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, 
$credentials.Password)  

$allColumns = $ctx.web.Fields 
$ctx.Load($allColumns) 
$ctx.executeQuery() 
$newColumn = $allColumns | where {$_.Title -eq $displayName} if($newColumn -ne $NULL)      
    {         
        Write-host "SharePoint Site Column already exists!"     
    }     
    else     
    {                 
        $FieldSchema = "<Field Type='DateTime' DisplayName='$displayName' Name='$internalName' Format='DateOnly' required='False' Group='$siteColumnGroupname'/>"         
    $newColumn = $allColumns.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)         
        $ctx.ExecuteQuery()              
        
        Write-host "SharePoint Site Column Created Successfully!"     
        }

Once you run the PowerShell script, it will create a Date and Time type SharePoint site column.

Person or Group:

We can also create a person or group type SharePoint site column using PowerShell in SharePoint Online. Here the field Type will be User, a few other parameters are like Type=’User’ ShowField=’ImnName’ List=’UserInfo’ UserSelectionMode=’PeopleOnly’ etc.


Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='SPGuides Site Columns' $internalName='SPGuidesManagerName' $displayName='Your Manager Name'  $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 

$credentials= Get-Credential 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, 
$credentials.Password)  

$allColumns = $ctx.web.Fields 
$ctx.Load($allColumns) 
$ctx.executeQuery() 
$newColumn = $allColumns | where {$_.Title -eq $displayName} if($newColumn -ne $NULL)      
    {         
        Write-host "SharePoint Site Column already exists!"     
    }     
    else     
    {                 
        $FieldSchema = "<Field Type='User' DisplayName='$displayName' Name='$internalName' ShowField='ImnName' List='UserInfo' UserSelectionMode='PeopleOnly' required='False' Group='$siteColumnGroupname'/>"         
         $newColumn = $allColumns.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)         
         $ctx.ExecuteQuery()              
         
         Write-host "SharePoint Site Column Created Successfully!"                         
         }

This is how we can create a person or group (people picker) column in SharePoint using PowerShell. Choice Type Column:

Now, let us check how to create choice type site column in SharePoint using PowerShell. Here we need to define Type=’Choice’ and then we need to provide the choice options like:


<CHOICES><CHOICE>HR</CHOICE><CHOICE>Finance</CHOICE><CHOICE>IT</CHOICE></CHOICES>
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = "https://tsinfo.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='SPGuides Site Columns' $internalName='SPGuidesDepartment' 
$displayName='Your Department'  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $credentials= Get-Credential 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, 

$credentials.Password)  
$allColumns = $ctx.web.Fields 
$ctx.Load($allColumns) 
$ctx.executeQuery() 
$newColumn = $allColumns | where {$_.Title -eq $displayName} if($newColumn -ne $NULL)      
    {         
        Write-host "SharePoint Site Column already exists!"     
    }     
    else     
    {                 
        $FieldSchema = "<Field Type='Choice' DisplayName='$displayName' Name='$internalName' required='False' Group='$siteColumnGroupname'><CHOICES><CHOICE>HR</CHOICE>
<CHOICE>Finance</CHOICE>
<CHOICE>IT</CHOICE>
</CHOICES></Field>"         
    $newColumn = $allColumns.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)                 
    $ctx.ExecuteQuery()  
                
    Write-host "SharePoint Site Column Created Successfully!"     
    }

This is how we can create a choice type column in SharePoint Online using PowerShell.


Conclusion:

In this SharePoint tutorial, we learned about SharePoint site column, how to create a site column in SharePoint? And how to create SharePoint site column programmatically using Rest API. Finally, we learned how to add SharePoint site column to an existing SharePoint list or document library. Site columns work same way in SharePoint Online and SharePoint 2013, SharePoint 2016 and SharePoint 2019.

Also, we learned sharepoint site columns vs list columns, sharepoint site columns best practices and how to create site column in sharepoint online using powershell.


Source: SPGuide.com

Recent Posts

See All