Posted on:
Categories: PowerShell;SharePoint
Description:
After discovering that migrating OneNote notebooks from one SharePoint location to another is difficult to do while maintaining the integrity of the notebooks (http://help.share-gate.com/article/907-onenote-migration), I needed to retrieve an inventory of all OneNote notebooks that reside on a collection of Office365 personal sites. 

Once I had a list of OneNote notebooks I could begin the process of migrating them in such a way that their integrity is maintained.

The following script uses the Client Side Object Model to find all OneNote notebooks. It will export a list of OneNote notebooks to a csv file.

​Make sure that the script is run in the same directory as the SharePoint Client dlls.

#Search for all OneNote files in a collection of Office365 sites

$global:notebooks = @()

Add-Type -Path "Microsoft.SharePoint.Client.dll" 
Add-Type -Path "Microsoft.SharePoint.Client.Runtime.dll" 

$cred = Get-Credential -Message "Enter your credentials for SharePoint Online:" 
$global:spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
 
Function Get-OneNoteInventory($url){
 $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
 $clientContext.Credentials = $spoCred
 $list = $clientContext.Web.Lists.GetByTitle("Documents")
 $query = New-Object Microsoft.SharePoint.Client.CamlQuery
 $query.ViewXml = "
 <View>
  <Query>
   <Where>
     <Eq>
     <FieldRef Name='HTML_x0020_File_x0020_Type' />
     <Value Type='Computed'>OneNote.Notebook</Value>
     </Eq>
     </Where>
  </Query>
 </View>"
 $listItems = $list.GetItems($query)
 $clientContext.Load($listItems)
 $clientContext.ExecuteQuery()
  
 $listUrl = $list.Context.Url
 foreach ($listItem in $listItems)
 {
  $fullUrl = $listUrl + $listItem["FileRef"]
  $o = new-object psobject
  $o | Add-Member -MemberType noteproperty -Name Name -value $listItem['Title']
  $o | Add-Member -MemberType noteproperty -Name Path -value $fullUrl
  $global:notebooks += $o
 }
}

$urls = @(
"https://contoso-my.sharepoint.com/personal/user1",
"https://contoso-my.sharepoint.com/personal/user2",
"https://contoso-my.sharepoint.com/personal/user3",
"https://contoso-my.sharepoint.com/personal/user4",
"https://contoso-my.sharepoint.com/personal/user5",
"https://contoso-my.sharepoint.com/personal/user6",
"https://contoso-my.sharepoint.com/personal/user7"
)

foreach($url in $urls){
 Get-OneNoteInventory -Url $url
}


$global:notebooks | export-csv "OneNote_Inventory.csv" -noTypeInformation