代码改变世界

How to Operate SharePoint User Alerts with PowerShell

  努力学习的小熊  阅读(1424)  评论(0编辑  收藏  举报

When you migrate list or site, the user alerts in the site will not be migrated together with the content. Below content can help you to do this task. But please take care of it, if you operate it on Prod environment.  

Enable or disable alerts for Web application

复制代码
## To enable alerts for Web application

$SPwebapp=Get-SPWebApplication "http://SharePointSite.com"

$SPwebapp.AlertsEnabled = $true

$SPwebapp.Update()

# To Disable alerts for a Web application

$SPwebapp.AlertsEnabled = $false

$SPwebapp.Update()
复制代码

 

Create Alert in SharePoint using PowerShell

复制代码
##### Create an New alert for an user #########

$SPsite = Get-SPSite "http://SharePointSite.com"

$SPweb=$SPsite.Rootweb

$SPlist=$SPweb.lists["Shared documents"]

$SPuser = $SPweb.EnsureUser('Domain\Salaudeen')

$SPnewAlert = $SPuser.Alerts.Add()

$SPnewAlert.Title = "My Custom Alert"

$SPnewAlert.AlertType=[Microsoft.SharePoint.SPAlertType]::List

$SPnewAlert.List = $SPlist

$SPnewAlert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::Email

$SPnewAlert.EventType = [Microsoft.SharePoint.SPEventType]::Add

$SPnewAlert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Immediate

$SPnewAlert.Update()

$SPweb.Dispose()

$SPSite.Dispose()
复制代码

 

Get all Alerts for an user in SharePoint with PowerShell

复制代码
##### Display All alerts for a Particular List ########

$SPWeb = Get-SPWeb "http://SharePointSite.com"

#Relative URL of list/document library. For lists "Lists/Tasks"

$SPListURL = "Shared Documents" 

foreach($alert in $SPWeb.Alerts)

{

    if($alert.ListUrl -eq $SPListUrl)

    {           

            "User Name    - " + $alert.User.Name

            "Title        - " + $alert.Title

            "Frequency    - " + $alert.AlertFrequency

            "Delivery Via - " + $alert.DeliveryChannels

            "Change Type  - " + $alert.eventtype

            Write-Host "=================================="

    }

}

$SPweb.Dispose()
复制代码

 

Create Alerts for All users in a Group

复制代码
##### Set alerts for all users in the SharePoint Group ######

$SPweb = Get-SPWeb "http://SharePointSite.com"

$SPgroup = $SPweb.Groups["SharePoint Owners"]

$SPlist = $SPweb.Lists["Shared Documents"]

foreach ($SPuser in $SPgroup.Users){

     $alert = $SPuser.Alerts.Add()

     $alert.Title = "My Alert"

     $alert.AlertType = [Microsoft.SharePoint.SPAlertType]::List

     $alert.List = $SPlist

     $alert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::Email

     $alert.EventType = [Microsoft.SharePoint.SPEventType]::Add

     $alert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Immediate

     $alert.Update()

}

$SPweb.Dispose()
复制代码

 

Update SharePoint Alerts using PowerShell

复制代码
#####  Making Changes in Existing Alerts ########

$SPsite = Get-SPSite "http://SharePointSite.com"

$SPweb=$SPsite.RootWeb

$SPuser=$SPweb.EnsureUser('Domain\Salaudeen')

$SPalertCollection=$SPuser.Alerts

foreach($alert in $SPalertCollection)

{

  $alert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Daily

  $alert.Update()

} 
复制代码

 

Get Alerts for a Particular User

复制代码
##### Get alerts for a particular user #########

$SPsite = Get-SPSite "http://SharePointSite.com"

$SPweb=$SPsite.RootWeb

$SPuser=$SPweb.EnsureUser('Domain\Salaudeen')

$SPalertCollection=$SPuser.Alerts

foreach($alert in $SPalertCollection)

{

 write-host -f Green $alert.Title

}
复制代码

 

Find All Alerts of an User in Entire Site collection

复制代码
##### Get the Alerts of Entire Site collection #####

$SPsiteCollection = Get-SPSite "http://SharePointSite.com"


# Iterate through all Webs in the Site Collection

foreach($SPweb in $SPsiteCollection.AllWebs) 

 {

   foreach($alert in $SPweb.Alerts)

    {

        Write-Host "Alerts List :" $alert.ListUrl

    Write-Host "Alerts Title :" $alert.title

    write-host "Subscribed User: " $alert.user

     }

 }
复制代码

 

Delete All SharePoint alerts from a List using powershell

复制代码
##### Delete All alerts for a specific List #####

$SPweb = Get-SPWeb "http://SharePointSite.com"

$SPlist = $SPweb.lists["Shared Documents"]

$IDS = ""

foreach($alert in $spweb.alerts)

{

    if($alert.ListID -eq $SPlist.ID)

    {

    $IDS += $alert.ID.tostring() + "|"

    }

    write-host -nonewline "*"

}

write-host "deleting..."

foreach($s in $IDS.Split("|"))

{

write-host -nonewline "*"

$spweb.alerts.delete([GUID]$s)

}
复制代码

 

Delete user alerts in SharePoint 2010 with PowerShell

复制代码
##### Remove all alerts for specific user from a Web Application #####

$SPwebApp = Get-SPWebApplication "http://SharePointSite.com"

$SpecificUser = "Domain\Salaudeen"

foreach ($SPsite in $SPwebApp.Sites)

    {

        # get the collection of webs

        foreach($SPweb in $SPsite.AllWebs)

        {

            $alerts = $SPweb.Alerts

            # if 1 or more alerts for a particular user, Make a note of them by copying their ID to an Array

            if ($alerts.Count -gt 0)

            {

                $myalerts = @()

                foreach ($alert in $alerts)

                {

                if ($alert.User -like $SpecificUser)

                {

                    $myalerts += $alert

                }

            }

            ### now we have alerts for this site, we can delete them

            foreach ($alertdel in $myalerts)

            {

                $alerts.Delete($alertdel.ID)

                write-host $alertdel.ID

            }

        }

    }

}
复制代码

 

To delete all alerts:

$SPweb.Alerts| foreach-object {$web.Alerts.Delete($_.Id)}

 

Filter the alerts for a single list:

#For for e.g. http://SharePoint.com/site/Assets"
$SPweb.Alerts| where-object {$_.ListUrl -like "Assets"} 

 

Find all the Alerts for a specific user across the web:

# ? is the alias for where-object cmdlet
$web.Alerts | ? {$_.UserId -like "Domain\Salaudeen"}

 

Get all the alerts for a user across the entire site collection:

$site.AllWebs | select -expand Alerts | ? {$_.UserId -like "Domain\Salaudeen"

 

Export User Alerts

复制代码
$site = Get-SPSite "http://2013portal"

$alertResultsCollection = @()

foreach ($web in $site.AllWebs) {

foreach ($alert in $web.Alerts){

$alertURL = $web.URL + "/" + $alert.ListUrl

$alertResult = New-Object PSObject

$alertResult |Add-Member -type NoteProperty -name "WebUrl" -Value $web.Url

$alertResult | Add-Member -type NoteProperty -name "ListURL" -value $alertURL

$alertResult | Add-Member -type NoteProperty -name "AlertTitle" -value $alert.Title

$alertResult | Add-Member -type NoteProperty -name "ListUrl" -value $alert.ListUrl

$alertResult | Add-Member -type NoteProperty -name "List" -value $alert.List

$alertResult | Add-Member -type NoteProperty -name "DeliveryChannel" -value $alert.DeliveryChannels

$alertResult | Add-Member -type NoteProperty -name "AlertType" -value $alert.AlertType

$alertResult | Add-Member -type NoteProperty -name "EventType" -value $alert.EventType

$alertResult | Add-Member -type NoteProperty -name "Frequency" -value $alert.AlertFrequency

$alertResult | Add-Member -type NoteProperty -name "AlertTime" -value $alert.AlertTime

$alertResult | Add-Member -type NoteProperty -name "SubscribedUser" -value $alert.User

$alertResultsCollection += $alertResult

}

}

$site.Dispose()

$alertResultsCollection

#Export to CSV

$alertResultsCollection | Export-CSV C:\Users\sp2013_farm_admin\Desktop\Alerts.csv
复制代码

 

Import User Alerts

复制代码
Import-Csv C:\Users\sp2013_farm_admin\Desktop\Alerts.csv |ForEach-Object{

$webUrl=$_.WebUrl

$listTitle=$_.List

$alertTitle=$_.AlertTitle

$subscribedUser=$_.SubscribedUser

$alertType=$_.AlertType

$deliveryChannel=$_.DeliveryChannel

$eventType=$_.EventType

$frequency=$_.Frequency

 

$web=Get-SPWeb $webUrl

$list=$web.Lists.TryGetList($listTitle)

$user = $web.EnsureUser($subscribedUser)

$newAlert = $user.Alerts.Add()

$newAlert.Title = $alertTitle

$newAlert.AlertType=[Microsoft.SharePoint.SPAlertType]::$alertType

$newAlert.List = $list

$newAlert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::$deliveryChannel

$newAlert.EventType = [Microsoft.SharePoint.SPEventType]::$eventType

$newAlert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::$frequency

if($frequency -ne "Immediate"){

$AlertTime=$_.AlertTime

$newAlert.AlertTime=$AlertTime

}

$newAlert.Update()

} 
复制代码

 

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2008-11-12 Building Document Workflows in SharePoint 2007 翻译
点击右上角即可分享
微信分享提示