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. Function Is-Column-Exists()
  5. {
  6. param
  7. (
  8. [Parameter(Mandatory=$true)] $TargetListFields,
  9. [Parameter(Mandatory=$true)] [string] $FieldName
  10. )
  11. #Check if the given field name
  12. $Field = $TargetListFields | where{$_.InternalName -eq $FieldName}
  13. if($Field) { return $true } else { return $false}
  14. }
  15. Function Create-List-Columns()
  16. {
  17. param
  18. (
  19. [Parameter(Mandatory=$true)] [string] $siteURL,
  20. [Parameter(Mandatory=$true)] [string] $destSiteURL,
  21. [Parameter(Mandatory=$true)] [string] $SourceListName,
  22. [Parameter(Mandatory=$true)] [string] $TargetListName
  23. )
  24. #Passing Credentials
  25. $credPath = 'D:\Arvind\safe\secretfile.txt'
  26. $fileCred = Import-Clixml -path $credpath
  27. $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($fileCred.UserName, $fileCred.Password)
  28. #Setup the source context
  29. $sourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
  30. $sourceCtx.Credentials = $Cred
  31. #Setup the destination Context
  32. $destCtx = New-Object Microsoft.SharePoint.Client.ClientContext($destSiteURL)
  33. $destCtx.Credentials = $Cred
  34. #Get the Source List and Target Lists
  35. $SourceList = $sourceCtx.Web.Lists.GetByTitle($SourceListName)
  36. $TargetList = $destCtx.Web.Lists.GetByTitle($TargetListName)
  37. #Load Source and Target Fields
  38. $SourceListFields = $SourceList.Fields
  39. $sourceCtx.Load($SourceListFields)
  40. $TargetListFields = $TargetList.Fields
  41. $destCtx.Load($TargetListFields)
  42. $sourceCtx.ExecuteQuery()
  43. $destCtx.ExecuteQuery()
  44. #Loop through each item in the source and Get column values, add them to target
  45. [int]$Counter = 1
  46. Foreach($SourceField in $SourceListFields)
  47. {
  48. #Handle Special Fields
  49. $FieldType = $SourceField.TypeAsString
  50. #Skip Read only, hidden fields, content type and attachments and fields is not User fields
  51. If((-Not ($SourceField.ReadOnlyField)) -and (-Not ($SourceField.Hidden)) -and ($SourceField.InternalName -ne "ContentType") -and ($SourceField.InternalName -ne "Attachments") )
  52. {
  53. Write-Host "Source Field" $Counter ":" $SourceField.InternalName -ForegroundColor Yellow
  54. $Counter +=1
  55. #check if column name exist in Target fields
  56. $IsColumnExist = Is-Column-Exists -TargetListFields $TargetListFields -FieldName $SourceField.InternalName
  57. if($IsColumnExist)
  58. {
  59. write-host $SourceField.Title " Column Exists in Given Target list" $TargetListName -f Magenta
  60. Write-Host "--------------------"
  61. #Write-Host $SourceField.SchemaXml
  62. $TargetField = $TargetListFields | where{$_.InternalName -eq $SourceField.InternalName}
  63. #Get Source Field Type and Target Field type
  64. $SourceFieldType = $SourceField.TypeAsString
  65. $TargetFieldType = $TargetField.TypeAsString
  66. #Write-Host $TargetField.SchemaXml
  67. if($SourceFieldType -ne $TargetFieldType)
  68. {
  69. Write-Host "Source Column type are" $SourceFieldType "and Target Column are " $TargetFieldType " which are not same!." -ForegroundColor Red
  70. Write-Host "Hence changing the Targeted list column type" -ForegroundColor Red
  71. #Update the TargetField by replacing destination column schema with source column schema
  72. $TargetField.SchemaXml = $SourceField.SchemaXml
  73. $TargetField.Update()
  74. $destCtx.ExecuteQuery()
  75. Write-Host "Target Column Type Changed Successfully !.............." -f Green
  76. }
  77. }
  78. else
  79. {
  80. write-host $SourceField.SchemaXml " Column Not Exists in Given Target list" $TargetListName -f Red
  81. $FieldSchema = $SourceField.SchemaXml
  82. #Add Columns to the List
  83. $NewField = $TargetList.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
  84. $destCtx.ExecuteQuery()
  85. Write-host "New Column Added to the List Successfully!" -ForegroundColor Green
  86. }
  87. }
  88. }
  89. }
  90. #Set Parameters
  91. $srcListSiteUrl = "Source site Url"
  92. $SourceListName = "Source List Name"
  93. $dstListSiteUrl = "Destination Site Url"
  94. $TargetListName = "Destination List Name"
  95. #Passing Credentials
  96. $credPath = 'D:\Arvind\safe\secretfile.txt'
  97. $fileCred = Import-Clixml -path $credpath
  98. #Call the function to copy list items
  99. 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.