Add SiteColumn To Content Type Using PowerShell Script In SharePoint 2013

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