![]() |
VOOZH | about |
The CData Cmdlets for OneNote 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 OneNote.
The Cmdlets are not only a PowerShell interface to OneNote, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete OneNote data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for OneNote. To access OneNote data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for OneNote.
Once you have acquired the necessary connection properties, accessing OneNote data in PowerShell can be enabled in three steps.
OneNote uses the OAuth authentication standard. To authenticate using OAuth, create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Help documentation for more information.
Install the module:
Install-Module OneNoteCmdlets
Connect:
$onenote = Connect-OneNote -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$id = "Jq74mCczmFXk1tC10GB" $notebooks = Select-OneNote -Connection $onenote -Table "Notebooks" -Where "Id = `'$Id`'" $notebooks
You can also use the Invoke-OneNote cmdlet to execute SQL commands:
$notebooks = Invoke-OneNote -Connection $onenote -Query 'SELECT * FROM Notebooks WHERE Id = @Id' -Params @{'@Id'='Jq74mCczmFXk1tC10GB'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for OneNote\lib\System.Data.CData.OneNote.dll")
Connect to OneNote:
$conn= New-Object System.Data.CData.OneNote.OneNoteConnection("OAuthClientId=MyApplicationId; OAuthClientSecret=MySecretKey; CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the OneNoteDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, notebook_displayName from Notebooks"
$da= New-Object System.Data.CData.OneNote.OneNoteDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.id $_.notebook_displayname
}
Update-OneNote -Connection $OneNote -Columns @('Id','notebook_displayName') -Values @('MyId', 'Mynotebook_displayName') -Table Notebooks -Id "MyId"
$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("UPDATE Notebooks SET Id='Jq74mCczmFXk1tC10GB' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OneNote.OneNoteParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-OneNote -Connection $OneNote -Table Notebooks -Columns @("Id", "notebook_displayName") -Values @("MyId", "Mynotebook_displayName")
$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("INSERT INTO Notebooks (Id) VALUES (@myId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OneNote.OneNoteParameter("@myId","Jq74mCczmFXk1tC10GB")))
$cmd.ExecuteNonQuery()
Remove-OneNote -Connection $OneNote -Table "Notebooks" -Id "MyId"
$cmd = New-Object System.Data.CData.OneNote.OneNoteCommand("DELETE FROM Notebooks WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OneNote.OneNoteParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the OneNote Data Provider to get started:
Download NowLearn more:
👁 Microsoft OneNote IconRapidly create and deploy powerful .NET applications that integrate with Microsoft OneNote data including Notebooks, Notes, Searches, Tags, and more!