top of page

Create a SharePoint Site Column using PowerShell

SharePoint is a tool that can be adjusted to fit the specific needs of different organizations. You can make it work better for your group by customizing certain parts of it. One way to do this is by using PowerShell to create what's called SharePoint site columns.


Read:

Create SharePoint Site Column using PowerShell

In this article, we'll learn how to do this using simple steps. This helps make SharePoint more useful and tailored to what your team needs.


Create a SharePoint Site Column using PowerShell


STEP 1: Open PowerShell

Open PowerShell with administrative privileges.


STEP 2: Install PnP PowerShell Module (if not already installed):

Run the following command to install the PnP PowerShell module:

Install-Module -Name SharePointPnPPowerShellOnline -Force

STEP 3: Connect to Your SharePoint Site:

Run the following command to connect to your site:

Connect-PnPOnline -Url <YourSiteURL> -UseWebLogin

Replace <YourSiteURL> with your actual SharePoint site URL.


STEP 4: Create a SharePoint Site Column

Choose the type of column you want to create.


Here we will guide you to create different site columns using PowerShell:

  1. Single line of text

  2. Multiple lines of text

  3. Date and Time

  4. Person or group

  5. Choice type column


1. Single Line of Text:

A Single Line of Text column allows users to input short text content, typically limited to a single line. It’s commonly used for fields like titles, names, or short descriptions. When you create a Single Line of Text column, you define its properties to suit your specific use case.


Key Characteristics:

  • Column Type: 'Text'

  • Usage Scenarios: Use this column for capturing concise information that doesn’t require multiline input.


# Load SharePoint Client DLLs
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"

# SharePoint site details
$siteUrl = "https://techplatform.sharepoint.com/sites/TSInfoClassic/"
$siteColumnGroupname = 'TechPlatform Site Columns'
$internalName = 'techplatformYourFirstName'
$displayName = 'Your First Name'

# Connect to SharePoint site
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = Get-Credential
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, $credentials.Password)

# Get all existing columns
$allColumns = $ctx.web.Fields
$ctx.Load($allColumns)
$ctx.ExecuteQuery()

# Check if the column already exists
$newColumn = $allColumns | Where-Object { $_.Title -eq $displayName }
if ($newColumn -ne $null) {
    Write-Host "SharePoint Site Column already exists!"
} else {
    # Create the site column
    $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!"
}

2. Multiple lines of text:

A multiple lines of text column allows users to input longer text content, such as descriptions, comments, or notes. Unlike single-line text columns, this type of column can handle larger amounts of text and supports line breaks.


Key Characteristics:

  • Column Type: 'Note' (for multiple lines of text)

  • Usage Scenarios: Use this column for capturing detailed information, user comments, or any content that requires more than a single line.


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://techplatform.sharepoint.com/sites/TSInfoClassic/" $siteColumnGroupname='TechPlatform Site Columns' $internalName='techplatformYourAddress' 
$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!"     
    }

3. Date and Time:

A Date and Time column allows you to capture both date and time information. It’s commonly used for tracking events, deadlines, or scheduled tasks. When you create a Date and Time column, you can further customize it by specifying additional parameters such as the display format (e.g., DateOnly).


Key Characteristics:

  • Column Type: 'DateTime'

  • Additional Parameters: You can set the format (e.g., 'DateOnly') to display only the date portion.


# Load SharePoint Client DLLs
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"

# SharePoint site details
$siteUrl = "https://techplatform.sharepoint.com/sites/TSInfoClassic/"
$siteColumnGroupname = 'TechPlatform Site Columns'
$internalName = 'techplatformJoiningDate'
$displayName = 'Your Joining Date'

# Connect to SharePoint site
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = Get-Credential
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, $credentials.Password)

# Get all existing columns
$allColumns = $ctx.web.Fields
$ctx.Load($allColumns)
$ctx.ExecuteQuery()

# Check if the column already exists
$newColumn = $allColumns | Where-Object { $_.Title -eq $displayName }
if ($newColumn -ne $null) {
    Write-Host "SharePoint Site Column already exists!"
} else {
    # Create the site column
    $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!"
}

4. Person or Group type column:

Here we will create a person or group-type SharePoint site column using PowerShell in SharePoint Online.


A person or group type column in SharePoint allows you to associate users or groups with specific items. You can use this column to track information related to people, such as project managers, team members, or stakeholders.


Key Parameters:

  • Type: Set to 'User'.

  • ShowField: Determines which field to display (e.g., 'ImnName' for the user’s display name).

  • List: Specifies the list that contains user information (usually 'UserInfo').

  • UserSelectionMode: Defines whether to allow only people ('PeopleOnly') or both people and groups ('PeopleAndGroups').


# Load SharePoint Client DLLs
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"

# SharePoint site details
$siteUrl = "https://techplatform.sharepoint.com/sites/TSInfoClassic/"
$siteColumnGroupname = 'TechPlatform Site Columns'
$internalName = 'techplatformManagerName'
$displayName = 'Your Manager Name'

# Connect to SharePoint site
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = Get-Credential
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, $credentials.Password)

# Get all existing columns
$allColumns = $ctx.web.Fields
$ctx.Load($allColumns)
$ctx.ExecuteQuery()

# Check if the column already exists
$newColumn = $allColumns | Where-Object { $_.Title -eq $displayName }
if ($newColumn -ne $null) {
    Write-Host "SharePoint Site Column already exists!"
} else {
    # Create the site column
    $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!"
}

5. Choice Type Column:

A choice-type column allows users to select from a predefined set of options. It’s commonly used for fields like department names, project statuses, or product categories. When you create a choice column, you define the available choices upfront, and users can then pick from this list when adding data to the column.


Key Characteristics:

  • Column Type: 'Choice'

  • Options: You specify the available choices during column creation.


Here’s how you can do it:

# Load SharePoint Client DLLs
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"

# SharePoint site details
$siteUrl = "https://techplatform.sharepoint.com/sites/TSInfoClassic/"
$siteColumnGroupname = 'TechPlatform Site Columns'
$internalName = 'techplatformDepartment'
$displayName = 'Your Department'

# Connect to SharePoint site
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = Get-Credential
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.Username, $credentials.Password)

# Get all existing columns
$allColumns = $ctx.web.Fields
$ctx.Load($allColumns)
$ctx.ExecuteQuery()

# Check if the column already exists
$newColumn = $allColumns | Where-Object { $_.Title -eq $displayName }
if ($newColumn -ne $null) {
    Write-Host "SharePoint Site Column already exists!"
} else {
    # Create the site column
    $FieldSchema = @"
        <Field Type='Choice' DisplayName='$displayName' Name='$internalName' Required='False' Group='$siteColumnGroupname'>
            <CHOICES>
                <CHOICE>HR</CHOICE>
                <CHOICE>Finance</CHOICE>
                <CHOICE>Engineering</CHOICE>
            </CHOICES>
        </Field>
"@
    $newColumn = $allColumns.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
    $ctx.ExecuteQuery()

    Write-Host "SharePoint Site Column Created Successfully!"
}

STEP 6: Verify the Column Creation:

Visit your SharePoint site and navigate to Site Settings. Under Site Columns, you should see your newly created column.


Happy coding!

Comentarios


bottom of page