2

I tried to export a SharePoint Online list to .csv by using this script: How to save a list in a csv file via Powershell

Below is the script:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint.Client') | Out-Null

$login = 'test@test.onmicrosoft.com'
$password = ConvertTo-SecureString 'password' -AsPlainText -Force
$webUrl = 'https://test.sharepoint.com/sites/test/'
$listName = 'My List'

$context = New-Object Microsoft.SharePoint.Client.ClientContext $webUrl
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials $login, $password

$web = $context.Web
$list = $web.Lists.GetByTitle($listName)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$items = $list.GetItems($query)

$context.Load($items)
$context.ExecuteQuery()

$items | Select @{ L = "Title"; E = { $_["Title"] } }, @{ L = "Created"; E = { $_["Created"] } } | Export-Csv -Path export.csv 

but I always get this Error:

Cannot contact site at the specified URL https://sharepoint.sharepoint.com/Lists/demolist/. There is no Web named "/Lists/demolist/_vti_bin/sites.asmx".

When I try to load the site "manually" I can load it.

4

To accomplish your requirement, you need following component locally.

Microsoft.SharePoint.Client.dll

So download it from here

I have tried following script and it worked.

Add-Type -Path "LocalPath\Microsoft.SharePoint.Client.dll"

$webUrl = "https://XXX.sharepoint.com"

$listName = "List Name"

$login = "xxx@yyy.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString


$context = New-Object Microsoft.SharePoint.Client.ClientContext $webUrl
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials $login, $password

$web = $context.Web
$list = $web.Lists.GetByTitle($listName)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$items = $list.GetItems($query)

$context.Load($items)
$context.ExecuteQuery()

$items | Select @{ L = "Title"; E = { $_["Title"] } }, @{ L = "Created"; E = { $_["Created"] } } | Export-Csv -Path d:\export.csv 
  • Do you know of any way to take the credentials/identity of the user account being used to run the script and use these to make the call to SharePoint Online? This would be ideal for us as it would prevent the need for storing the password in the script or for prompting the user. – Submits May 13 '16 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.