My Idea Factory - By Ludovic Perrichon

SharePoint, SP2013, AlertTemplates, C#, PowerShell

Create SharePoint Alerts by code

You probably have been to the article Alert Template from scratch, where I have described how to create a custom alert template from scratch and how to deploy for creating alert by clicking on "Alert me".

But now we want to create an alert with C# and PowerShell.

Before to keep going, you need to get the alert template name for the the alert you want to create.

Go to your alert templates xml file. By default :
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\XML\alertemplates.xml
Or your custom alert templates file if you made one.
Keep this file open to get the template names later.

You also have to keep in mind that creating an alert with code will not take the filter query inside the xml. So you can copy the one in xml, or you can create a brand new filter.

Create alert with C#

private static void CreateAlert(  
    SPWeb web,
    SPUser spUser, // The user who gonna have the alert
    SPList spList, // The SPList for the alert
    string AlertTitle, // Title of the alert
    string FilterQuery, // String which represent the Filter of the alert. Can be different from the one in xml.
    string AlertTemplateName, // Alert template name which is in the xml
    SPAlertFrequency Frequency, // Immediate, Daily or Weekly
    SPEventType EventType, // Add, modify, delete or All
    DateTime AlertTime) 
{
    SPAlertTemplate spTemplate = new SPAlertTemplate();
    spTemplate.Name = AlertTemplateName;

    SPAlert newAlert = web.Alerts.Add();
    newAlert.User = spUser;
    newAlert.AlertTemplate = spTemplate;
    newAlert.Title = AlertTitle;
    newAlert.AlertType = SPAlertType.List;
    newAlert.List = spList;
    newAlert.Filter = FilterQuery;
    newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email;
    newAlert.EventType = EventType;
    newAlert.AlertFrequency = Frequency;
    if (Frequency != SPAlertFrequency.Immediate)
    {
        newAlert.AlertTime = AlertTime;
    }
    newAlert.Update();
}

Create alert with Powershell

Add-PSSnapin Microsoft.SharePoint.PowerShell

$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$AlertsTemplateCollection =  new-object Microsoft.SharePoint.SPAlertTemplateCollection($contentService)

$SPweb = Get-SPWeb -Identity "http:/ /my_web_url.com"
$SPlist = $SPweb.Lists["MyListName"]

$SPuser = $SPweb.Users.GetByEmail("emailOfUser")
$alert = $SPuser.Alerts.Add()
$alert.Title = "Alert title"
$alert.AlertType = [Microsoft.SharePoint.SPAlertType]::List 
$alert.List = $SPlist
$alert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::Email 
$alert.EventType = [Microsoft.SharePoint.SPEventType]::All #Add, Delete, Modify or All
$alert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Immediate #Immediate, Daily or Weekly
$alert.Filter = "<Query><Neq><Value type='integer'><UserID /></Value><FieldRef name='Editor/New'/></Neq></Query>" # My CAML Filter, from xml or a new one

#Do no need alertime if you set AlertFrequency to Immediate
#$alert.AlertTime = Get-Date -Date "2017-01-10 15:00:00Z"

#For the alerttemplate you can get the one of your SPList
#$alert.AlertTemplate = $SPlist.AlertTemplate
#Or add your own
$alert.AlertTemplate = $AlertsTemplateCollection["SPAlertTemplateType.GenericList"] #SPAlertTemplateType.GenericList or another alert template name
$alert.Update()

Write-Host "Alert $alertTitle created for $user"

$SPweb.Dispose()