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.
-
- ############################
- #Blog:Tis add a column to List Content Type
- #Author; Gowtham Rajamanickam
- #Dat:25-03-2017
- #Version:1.0
- ############################
-
-
-
- Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
-
- # Required parameters
- $Username="[email protected]"
- $AdminPassword=Read-Host -Prompt "Password" -AsSecureString
- $AdminUrl="https://gowthamr.sharepoint.com"
- $ListTitle="Tutorial List"
- $SiteColumn="TutorialName"
-
-
-
- function AddcolummntolistCtype
- {
-
- param (
- [Parameter(Mandatory=$true,Position=1)]
- [string]$Username,
- [Parameter(Mandatory=$true,Position=2)]
- $AdminPassword,
- [Parameter(Mandatory=$true,Position=3)]
- [string]$Url,
- [Parameter(Mandatory=$true,Position=3)]
- [string]$ListTitle,
- [Parameter(Mandatory=$true,Position=3)]
- [string]$SiteColumn
- )
-
- $context=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
- $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)
- $context.Load($context.Web.Lists)
- $ll=$context.Web.Lists.GetByTitle($ListTitle)
- $context.Load($ll)
- $context.Load($ll.ContentTypes)
- $context.ExecuteQuery()
- $field=$context.Web.Fields.GetByInternalNameOrTitle($SiteColumn)
- foreach($cc in $ll.ContentTypes)
- {
-
- $link=new-object Microsoft.SharePoint.Client.FieldLinkCreationInformation
- $link.Field=$field
- $cc.FieldLinks.Add($link)
- $cc.Update($false)
- $context.ExecuteQuery()
- }
-
-
-
- $context.Dispose()
- }
-
-
-
- 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.