Use PowerShell to Iterate thru all SharePoint 2013 List Items Based on List View

Tom Molskow 386 Reputation points
2020-12-28T18:08:41.753+00:00

Hello Community!

I am working in SharePoint 2013 and I want to export all items and in a list based on the fields in a list view. This is what I have done so far (this only works for all list items and fields, it's not limited to fields in a list view):

# Get All List Items
# Writes Report to Output and CSV File

cls

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set config variables
$WebURL="https://bentest.bhcorp.ad/sites/testforms/"
$ListName ="Material Request Form"
#$ViewName = "Material Request Form Export"
$OutPutFile = "F:\Forms\Report"  + $(get-date -f dd_MM_yyyy_HH_mm_ss) + ".csv"

#Delete the Output File if exists
If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }

#Get Web and List Objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]

#Get the View
$view = $list.Views[$ViewName]

Write-host "Total Number of Items Found:"$List.Itemcount

#Define the CAML Query
$BatchSize = 500
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.ViewXml = @"
    <View Scope='Recursive'>
        <Query>
            <OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
        </Query>
        <RowLimit Paged='TRUE'>$BatchSize</RowLimit>
    </View>
"@

$Counter = 0
#Process items in batch
Do
{
    #Get List Items
    $ListItems = $List.GetItems($Query)
    $Counter =  $Counter+$ListItems.Count
    Write-Progress -PercentComplete ($Counter / ($List.ItemCount) * 100) -Activity "Exporting List Items of '$($List.Title)'" -Status "Processing Items $Counter of $($List.ItemCount)"

    #Array to Hold Result - PSObjects
    $ListItemCollection = @()

    #Get All List items
    $ListItems | ForEach {
        #write-host "Processing Item ID:"$_["ID"]

        $ExportItem = New-Object PSObject
        #Get Each field - Exclude hidden
        Foreach($Field in $_.Fields | Where {!$_.hidden})
        {
            $ExportItem | Add-Member -MemberType NoteProperty -name $Field.InternalName -value $_[$Field.InternalName] 
        }
        #Add the object with property to an Array
        $ListItemCollection += $ExportItem
    }
    #Export the result Array to CSV file
    $ListItemCollection | Export-CSV $OutPutFile -NoTypeInformation -append

    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
}
While($Query.ListItemCollectionPosition -ne $null)

Write-host "List data exported to CSV Successfully!" -f Green

Has anyone else solved this problem? If so please share examples and guidance.

Thanks!

Tom

Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2020-12-29T02:26:26.413+00:00

    Hi @Tom Molskow , thank you for posting in the Q&A forum.

    In line 56 ~line 59, just change your code to the below to make it limited to fields in the list view :

    Foreach($Field in $view.ViewFields | Where {!$_.hidden})  
             {  
                 $ExportItem | Add-Member -MemberType NoteProperty -name $Field -value $_[$Field]   
             }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.