Thursday, September 12, 2013

Script to download documents from SharePoint that match a certain view

Today I got a request to put some files from SharePoint on a USB stick.
My boss is currently in some jungle and although they have (satellite) SharePoint connection which is fine to conduct searches and "define which files they need", it's almost impossible to download 11 GB on.

That's why he told me which document-sets he wanted on a USB stick, for HIS boss to take with .

Our situation is that we have a few document libraries with about 20.000 files in them. Using views and document-sets to make sense of it all. Who needs folders (which are just a piece of metadata to group some files together… right )?!
That's all cewl.. But when you want to export a few document-sets and a few hundred other files grouped together via a value in a column which obviously doesn't exist when you click the "open in explorer" button.

After some thinking I decided to see if I can export the files to the file-system via PowerShell.
Pretty soon I found this article:

This article from Jack shows the basics for downloading items from a SP list via PowerShell. My only problem was that I don't need ALL files, just certain files.
So I created a view in SharePoint that matches my criteria. Basically a view per destination folder so that it makes a bit sense after exporting the files. (Folders.. You know.. The old-school stuff before SharePoint libraries !).

Quite soon I found out about the SPList Class & .GetItems method (sample script I had uses SpList.Items to iterate through each item in the list.
This method doesn't support filtering on a view or query, so I changed it to the SpList.GetItems  method.
You can provide a query or … VIEW to this method ! YAY…
Unfortunately I hit a snag.. In this part:
$filelocation = $item["ows_ServerUrl"]
This property "ows_ServerUrl" in not in the set of properties returned by the .GetItems method; so you can't use it to provide it to the download component.

After some research I came across this post:

And there I saw they provided the file (item) location using the following command:
 SPFile file = item.File;

So, I tried the same method in my script and … voilla ! It was working !!
Downloading the items that match the view !

Of course you can just change the filtering in the view, matching whatever files you wish to export from SharePoint and run the PowerShell… put them in a folder that matches the view and you're done !

I didn't come across any other example on how to do this, so I thought let's contribute and make a blog about it.

The final script:

#Script to download documents from SharePoint that match a certain view

#site name
$sourceurl="http://**/sites/newbusiness"

#view name
$viewname="tempj"

#destination location
$destinationfolder="e:\trial"

#view-name to filter the export
$spview = $list.views[$viewname]

$sourceweb = get-spweb $sourceurl
$sourcelist = "BRGM"
$list = $sourceweb.lists[$sourcelist];
$count = $list.items.Count

write-host "iterating $count items in list"
$listItems = $list.GetItems($spview)

write-host $listItems.Count
Write-host "Items in view"

foreach ($item in $listItems)

{
Write-host "Processing:"
Write-host $item.File.Name

if ($item.FileSystemObjectType -eq "File")
{
$filename = $item.File.Name

#downloadfile
$spFile = $item.File;
[string]$targetname = $filename
[string]$target = $destinationfolder + "\" + $targetname
[System.IO.File]::WriteAllBytes($target,$spFile.OpenBinary())
}

}