![]() |
VOOZH | about |
The CData Cmdlets for Office 365 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 Office 365.
The Cmdlets are not only a PowerShell interface to Office 365, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Office 365 data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Office 365. To access Office 365 data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Office 365.
Once you have acquired the necessary connection properties, accessing Office 365 data in PowerShell can be enabled in three steps.
Office 365 uses the OAuth authentication standard. To authenticate requests, obtain the OAuthClientId, OAuthClientSecret, and OAuthCallbackURL by registering an app with Office 365. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.
Install the module:
Install-Module Office365Cmdlets
Connect:
$office365 = Connect-Office365 -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -OAuthCallbackURL "$OAuthCallbackURL" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$userid = "54f34750-0d34-47c9-9949-9fac4791cddb" $files = Select-Office365 -Connection $office365 -Table "Files" -Where "UserId = `'$UserId`'" $files
You can also use the Invoke-Office365 cmdlet to execute SQL commands:
$files = Invoke-Office365 -Connection $office365 -Query 'SELECT * FROM Files WHERE UserId = @UserId' -Params @{'@UserId'='54f34750-0d34-47c9-9949-9fac4791cddb'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Office 365\lib\System.Data.CData.Office365.dll")
Connect to Office 365:
$conn= New-Object System.Data.CData.Office365.Office365Connection("OAuthClientId=MyApplicationId;OAuthClientSecret=MyAppKey;OAuthCallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the Office365DataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Size from Files"
$da= New-Object System.Data.CData.Office365.Office365DataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.name $_.size
}
Update-Office365 -Connection $Office365 -Columns @('Name','Size') -Values @('MyName', 'MySize') -Table Files -Id "MyId"
$cmd = New-Object System.Data.CData.Office365.Office365Command("UPDATE Files SET UserId='54f34750-0d34-47c9-9949-9fac4791cddb' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Office365 -Connection $Office365 -Table Files -Columns @("Name", "Size") -Values @("MyName", "MySize")
$cmd = New-Object System.Data.CData.Office365.Office365Command("INSERT INTO Files (UserId) VALUES (@myUserId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myUserId","54f34750-0d34-47c9-9949-9fac4791cddb")))
$cmd.ExecuteNonQuery()
Remove-Office365 -Connection $Office365 -Table "Files" -Id "MyId"
$cmd = New-Object System.Data.CData.Office365.Office365Command("DELETE FROM Files WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Office 365 Data Provider to get started:
Download NowLearn more:
👁 Office 365 IconThe Office 365 Data Provider gives developers the power to easily connect .NET applications to Microsoft Office 365 data including Outlook Mail, Contact, Calendar, Files, and more!