Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Last active August 29, 2015 14:18
Show Gist options
  • Save asadrefai/9034257a4203c05ade2f to your computer and use it in GitHub Desktop.
Save asadrefai/9034257a4203c05ade2f to your computer and use it in GitHub Desktop.
Add a site column into content type SharePoint 2013 using csom PowerShell
Try{
#Loading SharePoint Client side namespaces
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'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function AddField-InContentType()
{
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, fields and site objects
#
$web = $Context.Web
$fields = $Context.Site.RootWeb.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
{
#Check whether site column exists in site collection columns
#
Write-Host "Check if the site column" $FieldInternalName "is exist or not" -ForegroundColor Cyan
try
{
$field = $fields.GetByInternalNameOrTitle($FieldInternalName)
$Context.Load($field)
$Context.ExecuteQuery()
Write-Host "Site column" $FieldInternalName "exist" -ForegroundColor Cyan
}
catch
{
Write-Host "Site column" $FieldInternalName " does not exist in the list of site collection columns" -ForegroundColor Cyan
return
}
#Getting content types from the root site
#
$contentTypes = $Context.Site.RootWeb.ContentTypes
$Context.Load($contentTypes)
$Context.ExecuteQuery()
#Getting specified content type in which we need to add a column
#
$contentType = $contentTypes | Where {$_.Name -eq $ContentTypeName}
#Get Fields from the content type
#
$fieldCollection = $contentType.Fields
$Context.Load($fieldCollection);
$Context.ExecuteQuery();
#Check if field already exists in content type or not
#
$IsFieldExists = $false
foreach($fld in $fieldCollection)
{
Write-Host $fld.get_InternalName()
if($fld.get_InternalName() -eq $FieldInternalName){
$IsFieldExists = $true
}
}
if($IsFieldExists -eq $false)
{
#Get all the columns associated with content type
#
$fieldRefCollection = $contentType.FieldLinks;
$Context.Load($fieldRefCollection);
$Context.ExecuteQuery();
#Add a new column to the content type
#
Write-Host "Adding site column " $FieldInternalName "to the content type" -ForegroundColor Cyan
$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$fieldReferenceLink.Field = $field;
$contentType.FieldLinks.Add($fieldReferenceLink);
$contentType.Update($true)
$Context.Load($contentType)
$Context.ExecuteQuery()
Write-Host "Field added successfully" -ForegroundColor Green
}
else
{
Write-Host "Site Column " $FieldInternalName " already exists in content type" -ForegroundColor Cyan
}
}
catch
{
Write-Host ("Error while adding field in contet type. Error -->> " + $_.Exception.Message) -ForegroundColor Red
}
}
end{
$Context.Dispose()
}
}
$credentials = Get-Credential
$Url = 'http://YourSite.com'
AddField-InContentType $Url $credentials "Your Content Type Name" "YourSiteColumn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment