kwizcom banner advertisement

SharePoint Online: Add Site Column to List using PowerShell

Requirement: Add an existing site column to SharePoint list or library using PowerShell.

How to Add a Site Column to SharePoint Online List?
Site columns in SharePoint provides great re-usability without having to recreate same columns multiple times! Once created at top level site, we can utilize them to store metadata in any number of lists and libraries under the hierarchy. To add a site column to SharePoint list, follow these steps:
  • Go to List Settings >> Under Columns, Click on "Add from existing site columns" link.
  • From the available site columns, pick the required site column(s) and click on Add button.
    sharepoint online powershell to add site column to list
  • Click OK to save your changes.

Add Site Column to List or Library with PowerShell:
#Load SharePoint CSOM Assemblies
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"
 
#Variables
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$SiteColumnName="Department"

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

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$Web = $Ctx.web

#Get the list
$List=$Ctx.Web.Lists.GetByTitle($ListName)
    
#Get the Site column
$Field = $Web.Fields.GetByTitle($SiteColumnName)

#Add the site column to the list
$List.Fields.Add($Field)
$ctx.ExecuteQuery() 
    
Write-host "Site Column Added to the List Successfully!" -f Green
Lets add some error handling to the above code to handle scenarios, such as:
  • What if the given site column doesn't exist?
  • What if the given site column is already added to the list?
  • What if the given list doesn't exist? or what if the given credentials are invalid?, etc.
PowerShell to Add a Site Column to SharePoint List:
#Load SharePoint CSOM Assemblies
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"
 
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$SiteColumnName="Department"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.web

    #Get the Site column from web
    $SiteColumns = $Web.Fields
    $Ctx.Load($SiteColumns)
    $Ctx.ExecuteQuery()
    $SiteColumn = $SiteColumns | Where {$_.Title -eq $SiteColumnName}
    
    #Check if given site column exists
    if($SiteColumn -eq $Null)
    {
        Write-host "Site Column $SiteColumnName doesn't exists!" -f Yellow
    }
    else
    {
        #Get the list
        $List=$Ctx.Web.Lists.GetByTitle($ListName)

        #Check if the Filed exist in list already
        $Fields = $List.Fields
        $Ctx.Load($List)
        $Ctx.Load($Fields)
        $Ctx.ExecuteQuery() 
        $Field = $Fields | where {$_.Title -eq $SiteColumnName}
        if($Field -ne $NULL)  
        {
            Write-host "Column Name $SiteColumnName already exists in the list!" -f Yellow
        }
        else
        {
            #Add the site column to the list
            $NewColumn = $List.Fields.Add($SiteColumn)
            $ctx.ExecuteQuery() 
    
            Write-host "Site Column Added to the List Successfully!" -f Green
        }
    }
}
Catch {
    write-host -f Red "Error Adding Site Column to List!" $_.Exception.Message
}
SharePoint Online: Add Site Column to List using PowerShell SharePoint Online: Add Site Column to List using PowerShell Reviewed by Salaudeen Rajack on 3:42 AM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.