2

I am retrieving a change log for SharePoint Online site, but I am particularly interested in the who performed the change and on what item.

$changeQuery = New-Object Microsoft.SharePoint.Client.ChangeQuery $true,$true
$changeCollection = $ctx.Site.GetChanges($changeQuery)
$ctx.Load($changeCollection)
$ctx.ExecuteQuery()

I found this post, but when I export my changes to a csv, in Excel I don't see ItemId or UserId. I see those properties in the Powershell window as the script runs.

The Excel file looks like it looks, because not all elements include those properties. The columns in Excel come from the properties of the first retrieved item. Fine. I tried to use a rather clumsy workaround, by half-manually adding those properties to elements if they are not there:

foreach ($change in $changeCollection)
{
$change | Add-Member ItemName("Not Applicable")
$change | Add-Member ListName("Not Applicable")


if(-Not $change.IsPropertyAvailable("ItemId")) { $change | Add-Member ItemId("Not Applicable")} else { $change["ItemName"]=$ctx.Web.Lists.GetById($change.ListId).GetItemById($change.ItemId).LeafName}
if(! $change.IsPropertyAvailable("ListId")) { $change | Add-Member ListId("Not Applicable")}


Write-Output $change
}

but I am receiving an error:

Unable to index into an object of type Microsoft.SharePoint.Client.ChangeItem. At C:\Users\ivo\Desktop\wiki\technet\changecollection.ps1:53 char:104 + if(-Not $change.IsPropertyAvailable("ItemId")) { $change | Add-Member ItemId("No ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : CannotIndex

Questions: (answer to any of them will solve my trouble)
How to check if the property is available?
How to export to csv a jagged array/items with varied properties?

Of course any other more enlightened solution is most welcome.

  • Thanks the upvoter, but it never bides well when I get more upvotes than answers... – grisha Mar 13 '15 at 11:29
1

After error analysis I found out that the error was caused by calling improperly the item property. Item[Property] instead of Item.Property

Little things make you happy :) Please find below the working code as jagged array seems not to be a feasible solution:

if(! $change.IsPropertyAvailable("ItemId")) { $change | Add-Member ItemId("Not Applicable")} else { 
$ctx.Load($ctx.Web.Lists.GetById($change.ListId).GetItemById($change.ItemId))
$ctx.ExecuteQuery()
$change.ItemName=$ctx.Web.Lists.GetById($change.ListId).GetItemById($change.ItemId).LeafName}
if(! $change.IsPropertyAvailable("ListId")) { $change | Add-Member ListId("Not Applicable")}else { $change.ListName=$ctx.Web.Lists.GetById($change.ListId).Title}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.