How To Read Alerts For A Specific User Using CSOM In SharePoint Online/Office 365

As you all know, in the latest CSOM Nuget Package, there is a new addition of Alerts API. Below is SharePoint CSOM code to get a list of alerts for a given user. The CSOM code given below will give you an alert ID, alert title and its name. Please ensure that the user exists before you use the executequery command. You have to access the alerts property in the user object to get the list of alerts associated with them. 

Steps

  1. Login into Visual Studio.
  2. Ensure that you download the latest version of CSOM Nuget Package 16.1.6112.1200.
  3. Type the code given below in Program.cs file.
    1. string UserName = “i:0#.f|membership|” + userName;  
    2. User user = site.EnsureUser(UserName);  
    3. AlertCollection alerts = user.Alerts;  
    4. context.Load(alerts);  
    5. context.ExecuteQuery();  
    6. Console.WriteLine(alerts.Count);  
    7. foreach (Alert alert in alerts)  
    8. {  
    9.    Console.WriteLine(“Alert ID: {0}\n Alert Name:{1} “, alert.ID,alert.Title);  
    10. }  


X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now