Exporting a SharePoint view by powershell

by bronyx85 at 2013-02-18 07:05:05

Hi,

Is it possible to export a SharePoint view by Powershell?

I Know you can export a list or library, but I need to just export a sharepoint view,.

Thanks in advance.
by AlexBrassington at 2013-02-19 01:00:14
There’s a couple of approaches you can take here.

You can get/export bits of the view by using something like this:
$doclib.Views["All Documents"].SchemaXML | Export-CLIXml -Path "C]
You can then re-hydrate that on another machine, or you can export to other formats if they make more sense.

Or you can get at the same (I believe, i haven’t tried this one) information using the following format:

http://servername/site/vti_bin/owssvr. … LDATA=TRUE

That should give you the same schema xml, although if you’ve gone to the trouble of getting the GUID then going in through the first route seems to make the most sense for most cases.
by bronyx85 at 2013-02-19 05:35:44
Hi Alex .

Thanks for your reply.

My code was something on the lines of :

Add-pssnapin Microsoft.SharePoint.Powershell

$Web = Get-SPWeb "webAppUrl"

$spList = $web.Lists["ListName"]
$view = $splist.Views["Complete"]
$items = $splist.GetItems($view)
$items.Items | foreach {
$obj = New-Object PSObject -Property @{
“Column1” = $
["Title"]
"Column2" = $["ProjectName"]
}
$exportlist += $obj
$exportlist | Export-Csv -path ‘C:\Users\spadmin\Desktop\Filename.csv’


But for some reason, the CSV is empty. Is there anyway I can modify that script so it populates the CSV?
by bronyx85 at 2013-02-19 07:27:12
The following script ALSO works, but it exports the WHOLE list as a csv, I only want it to export a view…

But im not sure how to modify it to incorporate the ‘view’ element.

Any ideas?

$MyWeb = Get-SPWeb "WEB APP URL"
$MyList = $MyWeb.Lists["My Test"]
$exportlist = @()
$Mylist.Items | foreach {
$obj = New-Object PSObject -Property @{
“Column1” = $
["Title"]
"Column2" = $["Name"]
}
$exportlist += $obj
$exportlist | Export-Csv -path ‘C:\Users\spadmin\Desktop\Filename.csv’
}


Thanks in advance.
by AlexBrassington at 2013-02-19 09:03:06
If you’ve got a ‘status’ column then you can just use a where clause to replicate the view’s filtering (i.e. just select the complete entries).

$MyWeb = Get-SPWeb "WEB APP URL"
$MyList = $MyWeb.Lists["My Test"]
$exportlist = @()
$Mylist.Items | where {$
["status"] -eq 'Complete' } | foreach {
$obj = New-Object PSObject -Property @{
“Column1” = $["Title"]
"Column2" = $
["Name"]
}
$exportlist += $obj
$exportlist | Export-Csv -path 'C:\Users\spadmin\Desktop\Filename.csv'
}

It is possible in theory to get the query value from the view and parse that but it’s not going to be easy/quick.

Incidentally have you considered using the REST api instead of going in through the server object model? It may not be appropriate for your use-case but it’s a less tightly coupled approach for most cases…