Last active
August 29, 2015 14:05
-
-
Save devendrasv/5fb069a8f10a51044aeb to your computer and use it in GitHub Desktop.
This file contains 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
#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 columns | |
$Name ="SPJName" | |
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 | |
foreach ($item in $fields) | |
{ | |
if ($item.InternalName -eq $Name) | |
{ | |
Write-Host "Site column" $Name "is already exist" -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
} | |
Write-Host "creating site column" $Name -foregroundcolor black -backgroundcolor yellow | |
$fieldAsXML = | |
"<Field Type='Text' | |
DisplayName='SPJ Name' | |
Name='"+$Name+"' | |
ID='{545E5A0D-5553-4E02-97B3-F2D225B649D7}' | |
Group='SPJ Site Columns' />"; | |
$fieldOption = [Microsoft.SharePoint.Client.AddFieldOptions]::DefaultValue | |
$field = $fields.AddFieldAsXml($fieldAsXML, $true, $fieldOption) | |
$context.Load($field) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "Site column" $Name "created successfully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Error while creating site column" $Name $_.Exception.Message -foregroundcolor black -backgroundcolor Red | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment