Whenever I setup a development copy of SharePoint and want to do some testing on a content database I copied from production, I don’t want have the alerts still enabled. I have experimented with enabling and disabling via script, but it never seemed to work quite how I expected. Then I got the idea to just delete them since this is development copy of the content database anyway.
The PowerShell script below MUST be ran as ADMINISTRATOR on one of the front end servers for SharePoint 2010. If you don’t Run As Administrator, then you will get an error that you don’t have permissions to access the farm.
The script is theoretically simple. It goes to the SharePoint url you give it, opens up the web application, gets all the site collections. For each site collection it gets all the webs in that site collection (this is the same as recursively traversing the SharePoint tree of webs, but much wrapped up nicely in the AllWebs property). Then we loop through each web and get all the Webs. We then get all the ids of the alerts we want to delete. Next we delete each alert. If one fails because of a user not existing anymore, we put a valid user in its place and try again. This script should only take a short time to run (like seconds or few minutes, not hours).
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$numWebs = 0
$numDeleted = 0;
$sp = Get-SPWebApplication ("http://mySharePoint”)
foreach ($site in $sp.Sites)
{
foreach ($web in $site.AllWebs)
{
$numWebs++;
$alertIds = @();
foreach ($alert in $web.Alerts)
{
$alertIds += $alert.ID;
$numDeleted++;
}
foreach ($alertId in $alertIds)
{
try
{
$web.Alerts.Delete($alertId);
}
catch [system.exception]
{
# user no longer exists. Perhaps not migrated properly when changed domains, etc.
# In any case, we just need to update the alert so that it points to a valid user,
# then we can delete it.
if ($_.Exception.Message.Equals("User cannot be found."))
{
$alertToUpdate = $web.Alerts[$alertId];
$alertToUpdate.User = $web.SiteUsers[0];
$alertToUpdate.Update();
#try to delete again, but this time we have a valid user
$web.Alerts.Delete($alertId);
}
}
}
}
}
Write-Host "Num of Webs checked: " + $numWebs;
Write-Host "Num of Alerts deleted: " + $numDeleted;
To delete all alerts for a particular person, you may want to try this script. It was also the starting point for my script here.
1 comment:
Glad you found my script useful - do you know whether they work on SharePoint 2010 or 2013? I haven't had an opportunity to try - we went straight from SharePoint 2007 to Office 365, and I no longer administer the system.
Post a Comment