Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate SharePoint Excel Services Integration Tasks from PowerShell
Are you in search of a quick and easy way to access SharePoint Excel Services data from PowerShell? This article demonstrates how to utilize the SharePoint Excel Services Cmdlets for tasks like connecting to SharePoint Excel Services data, automating operations, downloading data, and more.
The CData Cmdlets for SharePoint Excel Services are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to SharePoint Excel Services.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to SharePoint Excel Services, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SharePoint Excel Services data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SharePoint Excel Services. To access SharePoint Excel Services data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SharePoint Excel Services.
Once you have acquired the necessary connection properties, accessing SharePoint Excel Services data in PowerShell can be enabled in three steps.
The URL, User, and Password properties, under the Authentication section, must be set to valid credentials for SharePoint Online, SharePoint 2010, or SharePoint 2013. Additionally, the Library property must be set to a valid SharePoint Document Library and the File property must be set to a valid .xlsx file in the indicated Library.
PowerShell
-
Install the module:
Install-Module ExcelServicesCmdlets
-
Connect:
$excelservices = Connect-ExcelServices -URL "$URL" -User "$User" -Password "$Password" -File "$File"
-
Search for and retrieve data:
$industry = "Floppy Disks" $account = Select-ExcelServices -Connection $excelservices -Table "Account" -Where "Industry = `'$Industry`'" $account
You can also use the Invoke-ExcelServices cmdlet to execute SQL commands:
$account = Invoke-ExcelServices -Connection $excelservices -Query 'SELECT * FROM Account WHERE Industry = @Industry' -Params @{'@Industry'='Floppy Disks'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SharePoint Excel Services\lib\System.Data.CData.ExcelServices.dll")
-
Connect to SharePoint Excel Services:
$conn= New-Object System.Data.CData.ExcelServices.ExcelServicesConnection("URL=https://myorg.sharepoint.com;[email protected];Password=password;File=Book1.xlsx;") $conn.Open()
-
Instantiate the ExcelServicesDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, AnnualRevenue from Account" $da= New-Object System.Data.CData.ExcelServices.ExcelServicesDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.annualrevenue }
Update SharePoint Excel Services Data
PowerShell
Update-ExcelServices -Connection $ExcelServices -Columns @('Name','AnnualRevenue') -Values @('MyName', 'MyAnnualRevenue') -Table Account -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("UPDATE Account SET Industry='Floppy Disks' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelServices.ExcelServicesParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert SharePoint Excel Services Data
PowerShell
Add-ExcelServices -Connection $ExcelServices -Table Account -Columns @("Name", "AnnualRevenue") -Values @("MyName", "MyAnnualRevenue")
ADO.NET
$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("INSERT INTO Account (Industry) VALUES (@myIndustry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelServices.ExcelServicesParameter("@myIndustry","Floppy Disks")))
$cmd.ExecuteNonQuery()
Delete SharePoint Excel Services Data
PowerShell
Remove-ExcelServices -Connection $ExcelServices -Table "Account" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.ExcelServices.ExcelServicesCommand("DELETE FROM Account WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.ExcelServices.ExcelServicesParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject