I need to write a script which I can launch (for example) at specific time and which will make a save of all lists on the SharePoint.
For now, the script is able to connect to SharePoint and retrieve all lists name.
I need help to write the part where, for each list I retrieved with the first part, the script will retrieve all fields of all items.
I just know to do it with hard written columns name (like that):
$array =@();
foreach ($listItem in $listItems)
{
$result = new-object psobject
$result | Add-Member -MemberType noteproperty -Name Title -value $listItem['Title'];
$result | Add-Member -MemberType noteproperty -Name Description -value $listItem['Description'];
[...]
$array += $result;
}
$CsvName1 = "Export_"+$ListTitle.replace(' ','_')+".csv"
$array| export-csv $CsvName1 -Encoding unicode;
Is there any way to retrieve all fields for all lists just like I want?
EDIT:
Here is the full code:
$SiteURL = "[URL]"
Write-Host "Loading CSOM libraries" -foregroundcolor black -backgroundcolor yellow
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Write-Host "Lib. successfully loaded !" -foregroundcolor black -backgroundcolor Green
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$User = Read-Host -Prompt "username"
$password = Read-Host -Prompt "Enter password" -AsSecureString
Write-Host "Trying to reach $SiteURL // Handling ClientContext..." -foregroundcolor black -backgroundcolor yellow
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $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 "Connected !" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Impossible to reach $SiteURL : bad password ?" -foregroundcolor black -backgroundcolor Red
return
}
$list = $web.Lists["Test Script"];
$fields = $list.Fields;
$list.Fields | select InternalName | Export-Csv -path ./blabla.csv;
Write-Host "Done !" -foregroundcolor black -backgroundcolor Green