Last active
August 29, 2015 14:06
-
-
Save devendrasv/2def9e9310f03ae94a08 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 | |
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 Tenant site $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 | |
$context.RequestTimeOut = 5000 * 60 * 10; | |
$web = $context.Web | |
$site = $context.Site | |
$context.Load($web) | |
$context.Load($site) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "authenticateed to SharePoint Online Tenant site $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Not able to authenticateed to SharePoint Online $_.Exception.Message" -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
#P.S. You can get site columns with single ExecuteQuery, but keeping the above code to connect to the site so that will be common for all samples | |
#You can try placing below 37 and 38 lines of below 21st line, it will still get the site columns by making single request to the server | |
#Get all site columns | |
Write-Host "Getting all site columns" -foregroundcolor black -backgroundcolor yellow | |
$fields = $web.Fields | |
$Context.Load($fields) | |
try | |
{ | |
$Context.ExecuteQuery() | |
Write-Host "Successfully retrived all site columns" -foregroundcolor black -backgroundcolor green | |
} | |
catch | |
{ | |
Write-Host "Error while retriving site columns" -foregroundcolor black -backgroundcolor Red | |
} | |
#Display Site columns | |
Write-Host "Displaying site columns with in the site started....." -foregroundcolor black -backgroundcolor yellow | |
foreach($field in $fields) | |
{ | |
Write-Host $field.Title | |
} | |
Write-Host "Displaying site columns Completed" -foregroundcolor black -backgroundcolor Green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment