What? A powershell script to export all lists from a web to csv files.
Why? Doesn't SharePoint offer multiple way to move data? Yes. And they never quite seem to do what I want. If I want to rebuild a development web after making schema changes to a list and I try to use any of the native SharePoint tools something always seems to go wrong. If you want to share development data with other developers using the native tools, again good luck. If you have multiple demonstrations and wish to install them on different farms and perhaps to different levels of the SharePoint product good luck. Sometimes these things work, others times they just bomb.
Using a .csv file to achieve the same thing means you have just the data without any of the baggage that often comes with using SharePoint.
This script accepts 2 parameters, the web url and the output path for the csv files (you must manually create the output path if it does not exist). It then get all lists and exports them to csv files where the names are the list title + .csv (yourListTitle.csv).
Here is the script:
Param(
[Parameter(Mandatory=$True)]
[string]$webUrl,
[Parameter(Mandatory=$True)]
[string]$outPath
)
$web = Get-SPWeb $webUrl
write-host ("Path: " + $outPath)
foreach($list in $web.Lists)
{
$exportlist = $null
$exportlist = @()
$list.Items | foreach {
$hash = $null
$hash = @{}
foreach($fld in $_.Fields ){
Try {
$hash.add($fld.Title, $_[$fld.Title])
}
Catch [System.Management.Automation.MethodInvocationException]
{
# Eating an error caused by duplicate column names.
}
Finally
{
#"End"
}
}
write-host ("Exported: " + $_.Title)
$obj = New-Object PSObject -Property $hash #@{
$exportlist += $obj
}
$expath = $outPath + '\' + $list.Title + '.csv'
$exportlist | Export-Csv -path $expath #$oPath
}
thanks it's a great script excellent :)
ReplyDeletebut I've a small problem : the Arabic letters shown as ?????????????
Try:
Delete1. downloading Notepad++
2. opening the notepad document using notepad++
3. Go to menu bar , click encoding , then character sets,arabic, windows 1256
If your text is readable you know the export script worked.
Thank you!! This was such a time-saver for me!!
ReplyDeleteYou're welcome, glad you found it useful.
DeleteThis script doesn't delete content from the site as it's exporting, does it?
ReplyDeleteNo.
DeleteHi, Thanks for posting this. We have a big SharePoint site and I am trying to export just a specific list on one of our subsites to CSV. When I try my web URL like: http:\\mySharePointSite\MySubSite
ReplyDeleteI get the error: Get-SPWeb : Get-SPWeb : Cannot find an SPWeb object with Id or Url
First, your slashes appear backwards:
Deletehttp:\\mySharePointSite\MySubSite
Change to:
http://mySharePointSite/MySubSite
Other possibilities:
Are you running the script on the SharePoint server hosting the site?
Make sure you are logged in to the server with an account that has access to the site.
how can I get all the versions of the items in the list where versions is turned on?
ReplyDeleteI would use the Client Side Object Model (CSOM) to for this task. The above script does not address how to do this.
DeleteHi there - I have files attached to each item, this code is not giving the file name. Any thought?
ReplyDeleteAttached files are located in a folder named "Attachments" then a sub-folder named by item id. You can find your attachments in that folder. You need to iterate the contents of that folder to get the attachment names.
DeleteIf I was going to try to get attachments and attachment names I would look at using the Client Side Object Model (CSOM) to accomplish that task.
Amazing script, thanks very much! :)
ReplyDeleteWorked for me!
the output file .csv sems like this
ReplyDelete#TYPE Selected.Microsoft.SharePoint.SPFieldLink
"Name"
"ContentType"
"SelectFilename"
"FileLeafRef"
"Created"
"Title"
"Modified"
"Modified_x0020_By"
"Created_x0020_By"
"ContentType"
"Title"
"FileLeafRef"
"ItemChildCount"
"FolderChildCount"
but no values any help please !!
Hi - what type of file would I save the script as?
ReplyDeleteFile type is powershell, extension .ps1
DeleteHi All,
ReplyDeletewhere do i need to pass the 2 parameters in teh script
the output file .csv sems like this
ReplyDelete#TYPE Selected.Microsoft.SharePoint.SPFieldLink
"Name"
"ContentType"
"SelectFilename"
"FileLeafRef"
"Created"
"Title"
"Modified"
"Modified_x0020_By"
"Created_x0020_By"
"ContentType"
"Title"
"FileLeafRef"
"ItemChildCount"
"FolderChildCount"
but no values any help please !!
Check how you are adding the parameters. You can edit the script and insert the parameters directly in the script if you prefer.
DeleteThank you for your solution! i used the script and all the lists i get are empty, why?
ReplyDeleteIs there an elegant way to strip out most or all of the metadata columns and just show the user-defined cols?
ReplyDelete