![]() |
VOOZH | about |
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.
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.
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'}
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-ExcelServices -Connection $ExcelServices -Columns @('Name','AnnualRevenue') -Values @('MyName', 'MyAnnualRevenue') -Table Account -Id "MyId"
$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()
Add-ExcelServices -Connection $ExcelServices -Table Account -Columns @("Name", "AnnualRevenue") -Values @("MyName", "MyAnnualRevenue")
$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()
Remove-ExcelServices -Connection $ExcelServices -Table "Account" -Id "MyId"
$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
Download a free trial of the SharePoint Excel Services Data Provider to get started:
Download NowLearn more:
👁 SharePoint Excel Services IconRapidly create and deploy powerful .NET applications that integrate with live Excel Spreadsheet content hosted on SharePoint server!