Last active
August 29, 2015 14:22
-
-
Save asadrefai/7a7537824ebcc21f2814 to your computer and use it in GitHub Desktop.
Add columns into content type from parent content type using csom PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function AddFieldFromParentContentType() | |
| { | |
| param( | |
| [Parameter(Mandatory=$true)][string]$url, | |
| [Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials, | |
| [Parameter(Mandatory=$true)][string]$ContentTypeName, | |
| [Parameter(Mandatory=$true)][string]$FieldInternalName | |
| ) | |
| begin{ | |
| try | |
| { | |
| #Get Client Object | |
| # | |
| $Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) | |
| $Context.Credentials = $Credentials | |
| #Load web object | |
| # | |
| $web = $Context.Web | |
| $fields = $web.Fields; | |
| $site = $context.Site | |
| $Context.Load($web) | |
| $Context.Load($site) | |
| $Context.Load($fields) | |
| $Context.ExecuteQuery() | |
| } | |
| catch | |
| { | |
| Write-Host "Error while getting context. Error -->> " + $_.Exception.Message -ForegroundColor Red | |
| } | |
| } | |
| process{ | |
| try | |
| { | |
| #Get content type collections from root web | |
| # | |
| $contentTypes = $Context.Site.RootWeb.ContentTypes | |
| $Context.Load($contentTypes) | |
| $Context.ExecuteQuery() | |
| #Get the desired content type from collection | |
| # | |
| $contentType = $contentTypes | Where {$_.Name -eq $ContentTypeName} | |
| #Get parent content type | |
| # | |
| $parentContentType = $contentType.Parent | |
| $Context.Load($parentContentType) | |
| $Context.ExecuteQuery() | |
| #Get fields of parent content type | |
| # | |
| $fieldCollection = $parentContentType.Fields | |
| $Context.Load($fieldCollection); | |
| $Context.ExecuteQuery(); | |
| #Check if field exists in parent content type | |
| # | |
| $IsFieldExists = $false | |
| foreach($fld in $fieldCollection) | |
| { | |
| if($fld.get_InternalName() -eq $FieldInternalName){ | |
| $IsFieldExists = $true | |
| $fieldToAdd = $fld | |
| } | |
| } | |
| if($IsFieldExists -eq $true) | |
| { | |
| #Get all the columns associated with content type | |
| # | |
| $fieldRefCollection = $contentType.Fields; | |
| $Context.Load($fieldRefCollection); | |
| $Context.ExecuteQuery(); | |
| #Check if field exists in content type | |
| # | |
| $IsFieldInContentTypeExists = $false | |
| foreach($fldInCT in $fieldRefCollection) | |
| { | |
| if($fldInCT.get_InternalName() -eq $fieldToAdd.InternalName){ | |
| $IsFieldInContentTypeExists = $true | |
| } | |
| } | |
| if($IsFieldInContentTypeExists -eq $false) { | |
| #Add a new column to the content type | |
| # | |
| $fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation | |
| $fieldReferenceLink.Field = $fieldToAdd; | |
| $contentType.FieldLinks.Add($fieldReferenceLink); | |
| $contentType.Update($true) | |
| $Context.Load($contentType) | |
| $Context.ExecuteQuery() | |
| Write-Host "Field" $FieldInternalName "from parent content type added successfully" -ForegroundColor Green | |
| Write-Host "" | |
| } | |
| else { | |
| Write-Host "Field in content type" $contentType.Name "already exists" -ForegroundColor Green | |
| Write-Host "" | |
| } | |
| } | |
| else | |
| { | |
| Write-Host "Field" $FieldInternalName "in parent content type" $parent.Name "do not exists" -ForegroundColor Cyan | |
| Write-Host "" | |
| } | |
| } | |
| catch | |
| { | |
| Write-Host ("Error while adding field in contet type. Error -->> " + $_.Exception.Message) -ForegroundColor Red | |
| } | |
| } | |
| end{ | |
| $Context.Dispose() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment