One of the all too common tasks that I get in my current position is to export SharePoint list data to a spreadsheet. You might be thinking, why do that when SharePoint provides Export to Excel out of the box? Generally you can get by with just using the out of the box functionality. However, if you are interested in doing more advanced filtering, including multiple lists, or even interfacing with an external system, then Powershell might be the way to go.
Prior to SharePoint 2013, you would generally have to log onto one of the SharePoint servers to execute a Powershell script against the SSOM (Server Side Object Model). Now, with SharePoint 2013, you can execute Powershell scripts directly from your desktop using the CSOM (Client Side Object Model)!
Before getting started, I do have one disclaimer. If you’re going to be doing this on a work computer, it’s likely that running scripts from your system is disabled. If that is the case you can do 1 of 2 things (2 that I can think of at least). Update the Execution Policy on your machine (which you’ll likely need to have an Administrator do…see here: https://technet.microsoft.com/library/hh847748.aspx), or run the script line by line. Now that I’ve gotten that out of the way, lets get started.
Follow the steps below create your script:
- On a SharePoint Server navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI (Assuming a default installation)
- Copy the following files to somewhere on your local machine: Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll
- Paste the below into an empty text file and save as Export-ListData.ps1
# Example: ./Export-ListData -url http://sp2013
param($url)
Add-Type -Path "E:\CSOM\Microsoft.SharePoint.Client.dll"
Add-Type -Path "E:\CSOM\Microsoft.SharePoint.Client.Runtime.dll"
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url);
$list = $clientContext.Web.Lists.GetByTitle("Tasks");
$query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Priority' /><Value Type='Choice'>(1) High</Value></Eq></Where></Query></View>"
$listItems = $list.GetItems($query);
$clientContext.Load($listItems);
$clientContext.ExecuteQuery();
$tasks =@();
foreach ($listItem in $listItems)
{
$o = new-object psobject
$o | Add-Member -MemberType noteproperty -Name Name -value $listItem['Title'];
$o | Add-Member -MemberType noteproperty -Name Status -value $listItem['Status'];
$o | Add-Member -MemberType noteproperty -Name PercentComplete -value $listItem['PercentComplete'];
$tasks += $o;
}
$tasks | export-csv "Tasks_Export.csv" -noTypeInformation;
The only thing that should need to changed in the above script would be the path references to the dlls that you copied to your machine. Other than that, to run this script, just pull up the Powershell command line, navigate to the directory containing your script, type ./Export-ListData.ps1 -url [YOUR SHAREPOINT SITE URL], and hit enter. If all goes well you should have a CSV file added to your current directory containing your exported data.
And that is essentially it. From here you can basically increase the complexity all you want by leveraging the CSOM and the raw functionality of Powershell. Have fun!
The post Using Powershell and the SharePoint 2013 CSOM to Export List Data appeared first on MetroStar Blog.