Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Created September 16, 2014 04:12
Show Gist options
  • Save devendrasv/0b61b3c04c6aa4d41934 to your computer and use it in GitHub Desktop.
Save devendrasv/0b61b3c04c6aa4d41934 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
#List name variable
$ListName ='Employees'
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
}
# use GetByTitle for getting list info by list name($ListName)
#P.S. You can get List views 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 41 and 42 lines of under 24 line, it will still get the list views with single request to the server
Write-Host "Getting $ListName list Views" -foregroundcolor black -backgroundcolor yellow
$employeesViews = $web.Lists.GetByTitle($ListName).Views
$context.Load($employeesViews)
try
{
$context.ExecuteQuery()
Write-Host "Successfully retrived $ListName list Views" -foregroundcolor black -backgroundcolor green
}
catch
{
Write-Host "Error while getting the $ListName List Views $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
}
Write-Host "Displaying $ListName List Views started....." -foregroundcolor black -backgroundcolor yellow
foreach($view in $employeesViews)
{
Write-Host $view.Title
}
Write-Host "Displaying $ListName List Views Completed" -foregroundcolor black -backgroundcolor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment