Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Last active August 29, 2015 14:05
Show Gist options
  • Save devendrasv/a9220fd372c619fe1a0c to your computer and use it in GitHub Desktop.
Save devendrasv/a9220fd372c619fe1a0c to your computer and use it in GitHub Desktop.
#Credentials to connect to office 365 site collection url
$url ="https://velegandla.sharepoint.com/sites/training"
$username="[email protected]"
$password="yourpassword"
$Password = $password |ConvertTo-SecureString -AsPlainText -force
# site column and content tye id
$Name ="SPJName"
$contentTypeId ="0x0100AEF836037845AD47AD8F87A8C5736D4E"
Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow
Set-Location $PSScriptRoot
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
Write-Host "CSOM libraries loaded successfully" -foregroundcolor black -backgroundcolor Green
Write-Host "authenticate to SharePoint Online site collection $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$Context.Credentials = $credentials
$web = $context.Web
$fields = $web.Fields;
$site = $context.Site
$context.Load($web)
$context.Load($fields)
$context.Load($site)
try
{
$context.ExecuteQuery()
Write-Host "authenticateed to SharePoint Online site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Not able to authenticateed to SharePoint Online site collection $url $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
return
}
Write-Host "Check if the site column" $Name "is exist or not" -foregroundcolor black -backgroundcolor yellow
try
{
$field = $fields.GetByInternalNameOrTitle($Name)
Write-Host "Site column" $Name "exist" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Site column" $Name "is not exist in the list of site collection columns" -foregroundcolor black -backgroundcolor Red
return
}
try
{
$contentType = $web.ContentTypes.GetById($contentTypeId)
}
catch
{
Write-Host "Content Type" $contentTypeId "not found in site content types collection." -foregroundcolor red
return
}
#Get all the columns associated with content type
$fieldRefCollection = $contentType.FieldLinks;
$context.Load($fieldRefCollection);
$context.ExecuteQuery();
#Add a new clumn to the content type
Write-Host "Adding site column" $Name "to the content type" -foregroundcolor black -backgroundcolor yellow
$fieldReferenceLink = New-Object Microsoft.SharePoint.Client.FieldLinkCreationInformation
$fieldReferenceLink.Field = $field;
$contentType.FieldLinks.Add($fieldReferenceLink);
$contentType.Update($true);
try
{
$context.ExecuteQuery();
Write-Host "Site column" $Name "added to the content type successfully" -foregroundcolor black -backgroundcolor green
}
catch
{
Write-Host "Error while adding site column" $Name "to the content type successfully" $_.Exception.Message -foregroundcolor black -backgroundcolor green
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment