3

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  
  • 1
    gallery.technet.microsoft.com/scriptcenter/… you can try to use Get-SPOListItems and Get-SPOListFields as a ready solution or check the code (it's a text file) for inspiration – grisha Oct 16 '15 at 11:28
  • Your question's title & body are contradictory. Can you modify it? – Atish Dipongkor - MVP Oct 16 '15 at 11:28
  • @grisha => Thanks for your help, but I need to use CSOM so all "Get-SP*" stuff can't work for me. – Gordon Amable Oct 16 '15 at 11:34
  • @AtishDipongkor => Which part is contradictory ? I don't understand. – Gordon Amable Oct 16 '15 at 11:34
  • 1
    @GordonAmable. These are not server cmdlets :) They are custom ones, using CSOM. Check inside the file how they are built. I think you just need to execute $lists=Get-SPOLists ; foreach($list in $lists) {get-spolistitems $list -IncludeAllProperties } – grisha Oct 16 '15 at 11:40
7

You can get all field of a list in the following way

$list = $web.Lists["Your List Name"];
$fields = $list.Fields;

Now all fields/columns are in the $fields. For example, following line will save all field names into a csv file.

$list.Fields | select InternalName  |  Export-Csv -path $path;

If you are needed to read a specific field, then follow.

$firstName = $fields["First Name"];

Assuming that display name of the field is First Name.

Update

I did not notice that you have asked for CSOM. My above solution is for SOM. You need to update your script. Few things are missing in your script. I run following lines line and they gave me output

$web = $context.Web
$site = $context.Site 
$list = $web.Lists.GetByTitle("Tender List");
$fields = $list.Fields;
$context.Load($web)
$context.Load($site)
$context.Load($list)
$context.Load($fields)
$context.ExecuteQuery()
$fields | select InternalName  |  Export-Csv -path "d:\blabla.csv";
  • Thanks for your answer ! I made a few test and I just have a blank file in result... May i show you the full code ? – Gordon Amable Oct 16 '15 at 11:44
  • Yes show full code in your question – Atish Dipongkor - MVP Oct 16 '15 at 11:45
  • How did you do? using mine or others? – Atish Dipongkor - MVP Oct 16 '15 at 11:55
  • Just showed full code in question with an edit ! x) – Gordon Amable Oct 16 '15 at 11:59
  • I still have a blank file, just wanted to show you the full code then you can check if I make a good use of your code sample – Gordon Amable Oct 16 '15 at 12:04

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.