SharePoint Online: How do I extract list changes with PowerShell?
Import-Module 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell' -DisableNameChecking
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$webUrl = "https://mysite.sharepoint.com/"
$username = "richard.leeman@mydomain.onmicrosoft.com"
$password = Read-Host -Prompt "Password for $username" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$web = $ctx.Web
$webs = $web.Webs
$lists = $web.Lists
$ctx.Load($lists)
$ctx.Load($webs)
$ctx.Load($web)
$ctx.ExecuteQuery()
$list = $lists| Where-Object { $_.Title -eq "My Documents"}
$dt = Get-Date
$dtUTC = $dt.ToUniversalTime()
$changeQuery = New-Object Microsoft.SharePoint.Client.ChangeQuery($true, $true)
$changeQuery.ChangeTokenStart = New-Object Microsoft.SharePoint.Client.ChangeToken
$changeQuery.ChangeTokenStart.StringValue = "1;3;" + $list.Id.ToString() + ";" + $dtUTC.AddHours(-10).Ticks.ToString() + ";-1"
$changeQuery.ChangeTokenEnd = New-Object Microsoft.SharePoint.Client.ChangeToken
$changeQuery.ChangeTokenEnd.StringValue = "1;3;" + $list.Id.ToString() + ";" + $dtUTC.Ticks.ToString() + ";-1"
$changes = $list.GetChanges($changeQuery)
$ctx.Load($changes)
$ctx.ExecuteQuery()
$addedCount = 0
$updatedCount = 0
$deleteCount = 0
if ($changes.AreItemsAvailable -eq $true)
{
$fileName = "sampleData.txt"
$changes > $fileName
$changes | ForEach-Object {
if (($_.ItemId -ne "") -and ($_.ItemId -ne $null))
{
if ($_.ChangeType -eq "Add") { $addedCount = $addedCount + 1 }
if ($_.ChangeType -eq "Update") { $updatedCount = $updatedCount + 1 }
if ($_.ChangeType -eq "DeleteObject") { $deleteCount = $deleteCount + 1 }
}
}
Start-Process notepad -ArgumentList $fileName
}
Write-Host "$addedCount items added"
Write-Host "$updatedCount items updated"
Write-Host "$deleteCount items deleted"
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$webUrl = "https://mysite.sharepoint.com/"
$username = "richard.leeman@mydomain.onmicrosoft.com"
$password = Read-Host -Prompt "Password for $username" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$web = $ctx.Web
$webs = $web.Webs
$lists = $web.Lists
$ctx.Load($lists)
$ctx.Load($webs)
$ctx.Load($web)
$ctx.ExecuteQuery()
$list = $lists| Where-Object { $_.Title -eq "My Documents"}
$dt = Get-Date
$dtUTC = $dt.ToUniversalTime()
$changeQuery = New-Object Microsoft.SharePoint.Client.ChangeQuery($true, $true)
$changeQuery.ChangeTokenStart = New-Object Microsoft.SharePoint.Client.ChangeToken
$changeQuery.ChangeTokenStart.StringValue = "1;3;" + $list.Id.ToString() + ";" + $dtUTC.AddHours(-10).Ticks.ToString() + ";-1"
$changeQuery.ChangeTokenEnd = New-Object Microsoft.SharePoint.Client.ChangeToken
$changeQuery.ChangeTokenEnd.StringValue = "1;3;" + $list.Id.ToString() + ";" + $dtUTC.Ticks.ToString() + ";-1"
$changes = $list.GetChanges($changeQuery)
$ctx.Load($changes)
$ctx.ExecuteQuery()
$addedCount = 0
$updatedCount = 0
$deleteCount = 0
if ($changes.AreItemsAvailable -eq $true)
{
$fileName = "sampleData.txt"
$changes > $fileName
$changes | ForEach-Object {
if (($_.ItemId -ne "") -and ($_.ItemId -ne $null))
{
if ($_.ChangeType -eq "Add") { $addedCount = $addedCount + 1 }
if ($_.ChangeType -eq "Update") { $updatedCount = $updatedCount + 1 }
if ($_.ChangeType -eq "DeleteObject") { $deleteCount = $deleteCount + 1 }
}
}
Start-Process notepad -ArgumentList $fileName
}
Write-Host "$addedCount items added"
Write-Host "$updatedCount items updated"
Write-Host "$deleteCount items deleted"
Comments
Post a Comment