Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Microsoft SharePoint Excel Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module Microsoft SharePoint ExcelCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module Microsoft SharePoint ExcelCmdlets;
You can then use the Connect-ExcelServices cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-ExcelServices -Url 'https://myorg.sharepoint.com' -User '[email protected]' -Password 'password' -File 'Book1.xlsx'
This section provides the details for connecting SharePoint On Premises.
Connecting to a Workbook
Regardless of which edition of SharePoint you are using, set File to the Excel workbook. This path is relative to the following properties:
- Library: The Shared Documents library is used by default. You can use this property to specify another document library in your organization; for example, if you want to connect to OneDrive for Business, set this property to "Documents".
- Folder: You can use this property to specify a path to a subfolder in a library. The path is relative to the library name specified in Library.
Connecting to Spreadsheet Data as Tables
The cmdlet detects the available tables based on the available objects in the underlying API.
The APIs surface different API objects; select the API based on the organization of your spreadsheets and your SharePoint version:
- OData: The OData API allows access to tables defined from Excel table objects (not ranges or spreadsheets), which you create by clicking Insert > Table in Excel. When connecting to the OData API, the cmdlet may not return any tables if you do not have table objects defined in your workbook. Set UseRESTAPI to true to connect to spreadsheets or ranges as tables.
- REST: The REST API enables access to tables defined from Excel table objects, ranges, and spreadsheets. This is the default API. Requesting a large number of rows from ranges and spreadsheets is restricted by the REST API, and the cmdlet limits the number of rows returned to 100 by default. The cmdlet also defaults to detecting column names from the first row; set Header to disable this.
With DefineTables additionally set, you can define tables based on ranges, using the Excel range syntax. A range that is too large will be restricted by the API.
See Data Model for more information on how the cmdlet detects tables and how to query them.
Connecting to SharePoint On Premises
Set the Url to your server's name or IP address. Additionally, set SharePointVersion and the authentication values.
To authenticate to SharePoint OnPremise, set AuthScheme to the authentication type and set User and Password, if necessary.
Note: When connecting to SharePoint On-Premises 2010, you must set UseRESTAPI to true.
-
Windows (NTLM)
This is the most common authentication type. As such, the cmdlet is preconfigured to use NTLM as the default; simply set the Windows User and Password to connect.
-
Kerberos
Please see Using Kerberos for details on how to authenticate with Kerberos.
-
Forms
This allows authentication through a custom authentication method, instead of Active Directory. To use this authentication type, set AuthScheme to FORMS and set the User and Password.
-
ADFS
Set the AuthScheme to ADFS. The following connection properties need to be set:
- User: Set this to the ADFS user.
- Password: Set this to ADFS password for the user.
- SSOLoginURL: Set this to the WS-trust endpoint of the ADFS server.
- RelyingParty: The value of the relying party identifier on the ADFS server for Sharepoint.
AuthScheme=ADFS;User=ADFSUserName;Password=ADFSPassword;SSOLoginURL=https://<authority>/adfs/services/trust/2005/usernamemixed;SSO Properties ='RelyingParty=urn:sharepoint:sp2016;';
-
Anonymous Access
Set the AuthScheme to NONE along with the URL.
Retrieving Data
The Select-ExcelServices cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-ExcelServices -Connection $conn -Table "Account" -Columns @("Id, Name") -Where "Industry='Floppy Disks'"The Invoke-ExcelServices cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-ExcelServices -Connection $conn -Table Account -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myAccountData.csv -NoTypeInformation
You will notice that we piped the results from Select-ExcelServices into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-ExcelServices -Url 'https://myorg.sharepoint.com' -User '[email protected]' -Password 'password' -File 'Book1.xlsx' PS C:\> $row = Select-ExcelServices -Connection $conn -Table "Account" -Columns (Id, Name) -Where "Industry = 'Floppy Disks'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Account", "Columns": [ ], "Id": "MyId", "Name": "MyName" }