1

I'm on SPO so I'm trying my luck with my limited CSOM knowledge. Basically I`d like to get the users have set up an alert for a list.

Im able to query the alert but the "User" property is only displayed as the class name "Microsoft.SharePoint.Client.User". Heres my code:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext $site 

$web = $ctx.Web
$ctx.Load($web)

$alerts = $web.Alerts
$ctx.Load($alerts)
$ctx.ExecuteQuery() 

This gives the following output if I call $alerts:

enter image description here

Unfortunately, I don`t know how to extract the user's email address from here.

Any ideas?

Thanks in advance for your answer.

0

I'm not 100% sure but I think you would do something like the below, as the User property is itself an object, so needs to be specifically requested, then you can access it's properties which will be stuff like the user name, email, etc.

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext $site 
$web = $ctx.Web
$ctx.Load($web)
$alerts = $web.Alerts
$ctx.Load($alerts)
$ctx.ExecuteQuery() 
foreach ($alert in $alerts) {
$alertUser = $alert.User
$ctx.Load($alertUser)
$ctx.ExecuteQuery()

# Use this to view all the object members then reference the ones you need
$alertUser | select-object -property *
$alertUserEmail = $alertUser.x
}

Where x is the name of the object property that has the email address, not sure what it is exactly.

Here`s the updated, working code:

$web = $ctx.Web
$ctx.Load($web)  

$siteUsers = $web.SiteUsers
$ctx.Load($siteUsers)

$alerts = $web.Alerts
$ctx.Load($alerts)

$ctx.ExecuteQuery()

foreach ($alert in $alerts) {
    foreach ($siteUser in $siteUsers){ 
        If ($siteUser.Id -eq $alert.UserId){
            $alert.Title
            $siteUser.id
            $siteUser.email
        }
    } 
}
  • I found this whole method of having to explicitly request things in CSOM the most difficult to get my head round too, but once you do you'll be amazed what you can do with it. There's probably a more efficient way of doing it than my example but it'll do for a quick script that isn't going to sit in memory for a long time. – Thomas Gass Sep 6 '17 at 11:05
  • Thanks. It pointed me into the right direction. I was able to get it to work and updated the solution in my initial post. – colonel_claypoo Sep 12 '17 at 11:17

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.