2

SharePoint alerts not working when added through PowerShell,

$url = "https://example.com/delete1"
$listName1 = "Discussion1"
$spWeb = Get-SPWeb $url -ErrorVariable err -ErrorAction SilentlyContinue
if ($err)
{
    Write-Host "createing web..."
    $spWeb = New-SPWeb -url $url -Template "STS#1" -Name "AlertsTest1" -Description "AlertsTest1"
}

$list = $spWeb.Lists.TryGetList($listName1)
if($list -eq $null)
{
    Write-Host "createing list..."
    $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DiscussionBoard
    $list = $spWeb.Lists.Add($listName1, $listName1, [int]$listTemplate)
}

$SPuser = $spWeb.EnsureUser('domain\user_SCAdmin')
$SPnewAlert = $SPuser.Alerts.Add()
$SPnewAlert.Title = "Powershell Alerts"
$SPnewAlert.AlertType=[Microsoft.SharePoint.SPAlertType]::List
$SPnewAlert.List = $list
$SPnewAlert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::Email
$SPnewAlert.EventType = [Microsoft.SharePoint.SPEventType]::All
$SPnewAlert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Immediate
$SPnewAlert.Update()


$spWeb.Dispose()

It works when I go to site and add a alert myself using User interface...

Also no alerts are displayed at weburl + /_layouts/15/MySubs.aspx...

1
  • it works when I add "i:0#.w|" to login name for some weird reasons "$spWeb.EnsureUser('i:0#.w|domain\user_SCAdmin')" Mar 26, 2018 at 14:03

1 Answer 1

2

The key to understand here is that you should NOT be passing in the user name in the format domain\username when you are using a claims-based web application. If you are only using Windows claims on the zone then the call will likely succeed when you pass in domain\username.

 $claim = New-SPClaimsPrincipal -Identity corp\servero -IdentityType WindowsSamAccountName
 $web.EnsureUser($claim.ToEncodedString());

The ToEncodedString() method returns a value like this: i:0#.w|domain\username

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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