Click here to Skip to main content
15,887,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a infopath form library which as all the form data in columns. I need a Powershell scrip to export that data into a excel file or a CSV file.

What I have tried:

I have used below code
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$MyWeb = Get-SPWeb -identity "http://cclportal/reservations/surveys"
$MyList = $web.Lists["CareTeam Application"]

$ListItemCollection = @()
 
 
$list.Items | foreach {
$ExportItem = New-Object PSObject
 
$ExportItem | Add-Member -MemberType NoteProperty -name "Employee Name" -value $_["Employee Name"]
 
$ExportItem | Add-Member -MemberType NoteProperty -Name "Home Address" -value $_["Home Address"]
 
#Add the object with property to an Array
 
$ListItemCollection += $ExportItem
 
}
 
#Export the result Array to CSV file
 $ListItemCollection | Export-CSV "c:\testexport.csv" -NoTypeInformation                       

#Dispose the web Object

$MyWeb.Dispose()

but i get this error below

Cannot index into a null array.
At C:\Users\_spsadmin\Desktop\Exporttoexcel.ps1:4 char:1
+ $MyList = $web.Lists["CareTeam Application"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\_spsadmin\Desktop\Exporttoexcel.ps1:12 char:1
+ $ExportItem | Add-Member -MemberType NoteProperty -name "Employee Name" -value $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Cannot index into a null array.
At C:\Users\_spsadmin\Desktop\Exporttoexcel.ps1:14 char:1
+ $ExportItem | Add-Member -MemberType NoteProperty -Name "Home Address" -value $_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Posted
Updated 8-Feb-17 10:43am

1 solution

$list.Items should be $MyList.Items as you do not have a declaration for $list.

Also, I would check $web.Lists["CareTeam Application"], This may also cause issues

As a safer option, I would use the ListID/GUID of the list.

To obtain the list ID, just goto the settings page of the list and it is in the URL

Hope this helps
 
Share this answer
 
Comments
Member 12990028 8-Feb-17 16:46pm    
Thank you.
Member 12990028 8-Feb-17 16:48pm    
Final code which is running and i got the data in the excel file. But its not populating "CreatedBy" columns value? Do you know why?


Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$MyWeb = Get-SPWeb -identity "http://cclportal/reservations/surveys"
$MyList = $MyWeb.Lists["CareTeam Application"]
$ListItemCollection = @()
 
 
$MyList.Items | foreach {
$ExportItem = New-Object PSObject
 
$ExportItem | Add-Member -MemberType NoteProperty -name "Created" -value $_["Created"]
 
$ExportItem | Add-Member -MemberType NoteProperty -Name "Requestors Email" -value $_["Requestors Email"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Employee Name" -value $_["Employee Name"]
$ExportItem | Add-Member -MemberType NoteProperty -Name "Status" -value $_["Status"]
 
#Add the object with property to an Array
 
$ListItemCollection += $ExportItem
 
}
 
#Export the result Array to CSV file
 
$ListItemCollection | Export-CSV "c:\testexport.csv" -NoTypeInformation

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900