Programmatically Creating Alerts in SharePoint 2010


Introduction:

SharePoint 2010 provides the capability to subscribe the content through a feature called "Alerts". Alerts can be subscribed for at one of the four levels list, document library, list item and document. This also enables you to be notified by e-mail of any changes made to the content of your SharePoint Web site. In this article we will be seeing how to create an alert for a particular user and for a specified document library using the SharePoint object model.

For configuring alerts in SharePoint 2010 through out of the box refer to my previous articlehttp://www.c-sharpcorner.com/UploadFile/anavijai/7593/  (Copy and paste the hyperlink).

SPAlert Class:

This class is used to represent an alert, which generates periodic e-mail or Short Message Service (SMS) notifications sent to a user about the list, list item, document, or document library to which the alert applies. Using this class we will be creating a new alert for a document library.

Steps involved:

  • Open Visual Studio 2010.

  • Go to File => New => Project.

  • Select Console Application template from the installed templates.

  • Add the following references. Microsoft.SharePoint

  • Add the following namespaces. using Microsoft.SharePoint;

  • Replace Program.cs with the following code.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. namespace Alerts

  7. {

  8. class Program   

  9. {

  10. static void Main(string[] args)       

  11. {

  12. using (SPSite site = new SPSite("http://serverName:22222/sites/TestSite/"))           

  13. {

  14. using (SPWeb web = site.RootWeb)               

  15. {

  16. SPList list=web.Lists.TryGetList("Shared Documents");

  17. SPUser user = web.EnsureUser(@"domainName\userName");

  18. SPAlert newAlert = user.Alerts.Add();                   

  19. newAlert.Title = "My Custom Alert";                   

  20. newAlert.AlertType=SPAlertType.List;                   

  21. newAlert.List = list;                    

  22. newAlert.DeliveryChannels = SPAlertDeliveryChannels.Email;                           

  23. newAlert.EventType = SPEventType.Add;                   

  24. newAlert.AlertFrequency = SPAlertFrequency.Immediate;                                                  

  25. newAlert.Update();               

  26. }           

  27. }       

  28. }   

  29. }

  30. }

  • Build the solution.

  • Hit F5.

  • Go to Site Actions => Site Settings =>Site Administration => User Alerts.


  • Select the user from the drop down to show all the alerts created by that user and click on "Update".


  • A new alert is created successfully using SharePoint object model.

  • An alternative method to check the newly created alert is to go to the Shared Documents (I have mentioned to create an alert for Shared Documents in my code) => in the ribbon interface, select Library Tools => Library => Alert Me.


  • Click on "Manage my alerts".You should be able to see the alert created as shown in the following.


SPAlertType Enumeration:

It is used to specify the type of item to which an alert applies. Here I am applying the alert to the document library, so I have chosen "List" as my alert type.






SPAlertDeliveryChannels Enumeration:

It is used to specify the method of delivering alerts.




SPAlertFrequency Enumeration:

It is used to specify the time interval for sending an alert about changes to a specific list, list item, document library, or document.





SPEventType Enumeration:

It is used to specify the type of event for a list, list item, document, or document library to which an alert responds.


Recent Posts

See All