SharePoint Online: PowerShell to Get Site Columns

Requirement: Get Site Columns in SharePoint Online using PowerShell

How to Get a Site Column in SharePoint Online using 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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$InternalName = "MyEditor"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)

    #Get the Site Column
    $SiteColumn = $Web.Fields.GetByInternalNameOrTitle($InternalName)
    $Ctx.Load($SiteColumn)
    $Ctx.ExecuteQuery()

    #Get the Site Column's Name,Type, ID and Group
    $SiteColumn | Select Title, TypeDisplayName, ID, Group
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}

SharePoint Online: PowerShell to Get All Site Columns
Let's get all site columns from a SharePoint Online site.
#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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)

    #Get All Site Columns of the Web
    $SiteColumns = $Web.Fields
    $Ctx.Load($SiteColumns)
    $Ctx.ExecuteQuery()  

    #Get the Site Column Name and Group
    $SiteColumns | Select Title, Group  
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}
This script gets all site column names and its group.

PowerShell to Export Site Columns to CSV File
Let's extract all site columns data in a SharePoint Online site and export to CSV using PowerShell script.
#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"
    
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$CSVPath ="C:\Temp\SiteColumns.csv"

#Get Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)

    #Get All Site Columns of the Web
    $SiteColumns = $Web.Fields
    $Ctx.Load($SiteColumns)
    $Ctx.ExecuteQuery()  

    $DataCollection = @()
    #Loop through each site column
    ForEach($Field in $SiteColumns)
    {
        #Collect data        
        $Data = New-Object PSObject -Property @{
            FieldName  = $Field.Title
            InternalName = $Field.InternalName
            Group = $Field.Group
            SchemaXML = $Field.SchemaXml
            ID = $Field.Id
            Type = $Field.TypeDisplayName
        }
        $DataCollection += $Data          
    }
    #Export Site columns data to CSV file
    $DataCollection | Export-Csv -Path $CSVPath -NoTypeInformation -Force
    Write-host -f Green "Site Columns Data Export to CSV!"
}
Catch {
    write-host -f Red "Error: " $_.Exception.Message
}
and the result:
sharepoint online powershell get site columns

If you want to backup-restore (or export-import) site columns in SharePoint Online, use my another script: SharePoint Online: Export-Import Site Columns using PowerShell
SharePoint Online: PowerShell to Get Site Columns SharePoint Online: PowerShell to Get Site Columns Reviewed by Salaudeen Rajack on July 09, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.