The example given below adds an existing site column to all the list content types in a particular list.

Before running the script, load the mandatory DLL given below.
  1. ############################
  2. #Blog:Tis add a column to List Content Type
  3. #Author; Gowtham Rajamanickam
  4. #Dat:25-03-2017
  5. #Version:1.0
  6. ############################
  7. Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
  8. Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  9. Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
  10. # Required parameters
  11. $Username="[email protected]"
  12. $AdminPassword=Read-Host -Prompt "Password" -AsSecureString
  13. $AdminUrl="https://gowthamr.sharepoint.com"
  14. $ListTitle="Tutorial List"
  15. $SiteColumn="TutorialName"
  16. function AddcolummntolistCtype
  17. {
  18. param (
  19. [Parameter(Mandatory=$true,Position=1)]
  20. [string]$Username,
  21. [Parameter(Mandatory=$true,Position=2)]
  22. $AdminPassword,
  23. [Parameter(Mandatory=$true,Position=3)]
  24. [string]$Url,
  25. [Parameter(Mandatory=$true,Position=3)]
  26. [string]$ListTitle,
  27. [Parameter(Mandatory=$true,Position=3)]
  28. [string]$SiteColumn
  29. )
  30. $context=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  31. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
  32. $context.Load($context.Web.Lists)
  33. $ll=$context.Web.Lists.GetByTitle($ListTitle)
  34. $context.Load($ll)
  35. $context.Load($ll.ContentTypes)
  36. $context.ExecuteQuery()
  37. $field=$context.Web.Fields.GetByInternalNameOrTitle($SiteColumn)
  38. foreach($cc in $ll.ContentTypes)
  39. {
  40. $link=new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
  41. $link.Field=$field
  42. $cc.FieldLinks.Add($link)
  43. $cc.Update($false)
  44. $context.ExecuteQuery()
  45. }
  46. $context.Dispose()
  47. }
  48. AddcolummntolistCtype -Username $Username -AdminPassword $AdminPassword -Url $AdminUrl -ListTitle $ListTitle -SiteColumn $SiteColumn
Conclusion

Was my blog helpful?

If so, please let me know at the bottom of this page. If not, let me know what was confusing or missing and I will use your feedback to double-check the facts, add info and update this blog.