Create a Column in a Destination Site Using PowerShell in SharePoint Online

Introduction 

 
This blog will help you create the remaining columns in a destination SharePoint site using Powershell.
 
Sometimes we have to manually create many columns in the destination SharePoint sites, resulting in incorrect column creation or incorrect types of columns.
 
Otherwise, we save the list as a template with content and upload the same template in a different site to create a list. However, there are a couple of scenarios where we are not able to save the list as a template. In such cases, we can use the below script to create a column in the destination site after creating the list with a default column (Title).
 
There are prerequisites to run the script on Sharepoint online, as we are required to pass the credentials to authenticate them. It may help to refer to my blog, Copy List Items from One Site to Another With Versions using Powershell.
 
Copy and run the script by changing the required parameters.
  1. #Set Parameters    
  2. $srcListSiteUrl = "Source site Url"        
  3. $SourceListName = "Source List Name"         
  4. $dstListSiteUrl = "Destination Site Url"        
  5. $TargetListName = "Destination List Name"    
Complete script 
  1. #Load SharePoint CSOM Assemblies  
  2. Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'  
  3. Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'  
  4.   
  5. Function Is-Column-Exists()   
  6. {  
  7.     param  
  8.     (  
  9.         [Parameter(Mandatory=$true)] $TargetListFields,  
  10.         [Parameter(Mandatory=$true)] [string] $FieldName  
  11.     )  
  12.      #Check if the given field name  
  13.     $Field = $TargetListFields | where{$_.InternalName -eq $FieldName}  
  14.     if($Field)  { return $true } else { return $false}  
  15. }  
  16.   
  17. Function Create-List-Columns()  
  18. {  
  19.      param  
  20.     (  
  21.         [Parameter(Mandatory=$true)] [string] $siteURL,  
  22.         [Parameter(Mandatory=$true)] [string] $destSiteURL,  
  23.         [Parameter(Mandatory=$true)] [string] $SourceListName,  
  24.         [Parameter(Mandatory=$true)] [string] $TargetListName  
  25.     )  
  26.       
  27.     #Passing Credentials  
  28.     $credPath = 'D:\Arvind\safe\secretfile.txt'  
  29.     $fileCred = Import-Clixml -path $credpath  
  30.     $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($fileCred.UserName, $fileCred.Password)  
  31.       
  32.     #Setup the source context  
  33.     $sourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  34.     $sourceCtx.Credentials = $Cred  
  35.           
  36.     #Setup the destination Context  
  37.     $destCtx = New-Object Microsoft.SharePoint.Client.ClientContext($destSiteURL)  
  38.     $destCtx.Credentials = $Cred  
  39.   
  40.     #Get the Source List and Target Lists  
  41.     $SourceList = $sourceCtx.Web.Lists.GetByTitle($SourceListName)  
  42.     $TargetList = $destCtx.Web.Lists.GetByTitle($TargetListName)  
  43.   
  44.     #Load Source and Target Fields  
  45.     $SourceListFields = $SourceList.Fields  
  46.     $sourceCtx.Load($SourceListFields)  
  47.           
  48.     $TargetListFields = $TargetList.Fields  
  49.     $destCtx.Load($TargetListFields)  
  50.       
  51.     $sourceCtx.ExecuteQuery()  
  52.     $destCtx.ExecuteQuery()    
  53.   
  54.     #Loop through each item in the source and Get column values, add them to target  
  55.     [int]$Counter = 1  
  56.   
  57.     Foreach($SourceField in $SourceListFields)  
  58.     {  
  59.         #Handle Special Fields  
  60.         $FieldType  = $SourceField.TypeAsString  
  61.         #Skip Read only, hidden fields, content type and attachments and fields is not User fields  
  62.         If((-Not ($SourceField.ReadOnlyField)) -and (-Not ($SourceField.Hidden)) -and ($SourceField.InternalName -ne  "ContentType") -and ($SourceField.InternalName -ne  "Attachments") )   
  63.         {  
  64.             Write-Host "Source Field" $Counter ":" $SourceField.InternalName -ForegroundColor Yellow  
  65.             $Counter +=1  
  66.             #check if column name exist in Target fields  
  67.             $IsColumnExist = Is-Column-Exists -TargetListFields $TargetListFields -FieldName $SourceField.InternalName  
  68.             if($IsColumnExist)   
  69.             {  
  70.               write-host $SourceField.Title " Column Exists in Given Target list" $TargetListName -f Magenta  
  71.               Write-Host "--------------------"  
  72.               #Write-Host $SourceField.SchemaXml  
  73.               $TargetField = $TargetListFields | where{$_.InternalName -eq $SourceField.InternalName}  
  74.               #Get Source Field Type and Target Field type  
  75.               $SourceFieldType  = $SourceField.TypeAsString  
  76.               $TargetFieldType  = $TargetField.TypeAsString  
  77.               #Write-Host $TargetField.SchemaXml  
  78.               if($SourceFieldType -ne $TargetFieldType)   
  79.               {  
  80.                 Write-Host "Source Column type are" $SourceFieldType "and Target Column are " $TargetFieldType " which are not same!." -ForegroundColor Red  
  81.                 Write-Host "Hence changing the Targeted list column type" -ForegroundColor Red   
  82.                 #Update the TargetField by replacing destination column schema with source column schema  
  83.                 $TargetField.SchemaXml = $SourceField.SchemaXml  
  84.                 $TargetField.Update()  
  85.                 $destCtx.ExecuteQuery()  
  86.                 Write-Host "Target Column Type Changed Successfully !.............." -f Green  
  87.               }  
  88.             }  
  89.             else   
  90.             {  
  91.                 write-host $SourceField.SchemaXml " Column Not Exists in Given Target list" $TargetListName -f Red  
  92.                 $FieldSchema = $SourceField.SchemaXml  
  93.                 #Add Columns to the List  
  94.                   
  95.                 $NewField = $TargetList.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)  
  96.                 $destCtx.ExecuteQuery()  
  97.                 Write-host "New Column Added to the List Successfully!" -ForegroundColor Green  
  98.             }  
  99.   
  100.         }  
  101.           
  102.     }  
  103. }  
  104.   
  105. #Set Parameters  
  106. $srcListSiteUrl = "Source site Url"      
  107. $SourceListName = "Source List Name"       
  108. $dstListSiteUrl = "Destination Site Url"      
  109. $TargetListName = "Destination List Name"  
  110.   
  111. #Passing Credentials  
  112. $credPath = 'D:\Arvind\safe\secretfile.txt'  
  113. $fileCred = Import-Clixml -path $credpath  
  114.   
  115. #Call the function to copy list items  
  116. Create-List-Columns -siteURL $srcListSiteUrl -destSiteURL $dstListSiteUrl -SourceListName $SourceListName -TargetListName $TargetListName  
Note
 
If the remaining columns had any lookup column, then a corrupt lookup column will be created on the destination site. To correct the corrupt lookup column, please refer to my blog, Repair Lookup Column In SharePoint Online Using PowerShell.