Monday, April 22, 2013

Exporting SharePoint List Items to a CSV File via Powershell

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

}

21 comments:

  1. thanks it's a great script excellent :)
    but I've a small problem : the Arabic letters shown as ?????????????

    ReplyDelete
    Replies
    1. Try:
      1. 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.

      Delete
  2. Thank you!! This was such a time-saver for me!!

    ReplyDelete
  3. This script doesn't delete content from the site as it's exporting, does it?

    ReplyDelete
  4. Hi, 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
    I get the error: Get-SPWeb : Get-SPWeb : Cannot find an SPWeb object with Id or Url

    ReplyDelete
    Replies
    1. First, your slashes appear backwards:
      http:\\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.

      Delete
  5. how can I get all the versions of the items in the list where versions is turned on?

    ReplyDelete
    Replies
    1. I would use the Client Side Object Model (CSOM) to for this task. The above script does not address how to do this.

      Delete
  6. Hi there - I have files attached to each item, this code is not giving the file name. Any thought?

    ReplyDelete
    Replies
    1. Attached 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.

      If 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.

      Delete
  7. Amazing script, thanks very much! :)

    Worked for me!

    ReplyDelete
  8. the output file .csv sems like this
    #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 !!

    ReplyDelete
  9. Hi - what type of file would I save the script as?

    ReplyDelete
  10. Hi All,

    where do i need to pass the 2 parameters in teh script

    ReplyDelete
  11. the output file .csv sems like this
    #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 !!

    ReplyDelete
    Replies
    1. Check how you are adding the parameters. You can edit the script and insert the parameters directly in the script if you prefer.

      Delete
  12. Thank you for your solution! i used the script and all the lists i get are empty, why?

    ReplyDelete
  13. Is there an elegant way to strip out most or all of the metadata columns and just show the user-defined cols?

    ReplyDelete