create sharepoint alert

I can create an alert for a user, but how can I do it for a security group ?
the code I attach here works for the user, but how do I do it for a GROUP ?
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace Create_Alerts_Sharepoint
{
    public class Class1
    {

        static void Main(string[] args)
        {
           using (SPSite site = new SPSite("http://shdev01/kp"))
           {
                 using (SPWeb web = site.OpenWeb()) 
               { 
                    SPUser user = web.SiteUsers[@"DOMAIN\user"];
              SPAlert newAlert = user.Alerts.Add();
              newAlert.Title = "Latest News";
              newAlert.AlertType = SPAlertType.List;
              newAlert.List = web.Lists["Carrefour"];
              newAlert.EventType = SPEventType.All;
              newAlert.AlertFrequency = SPAlertFrequency.Daily;
                    newAlert.AlertTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 12, 0, 0);
                    newAlert.AlertTime = newAlert.AlertTime.AddDays(1);
                     newAlert.Update(false); 
                } 
            }
        }
    }
}

Open in new window

dekempeneerAsked:
Who is Participating?
 
larrbeCommented:
Hi,
I just tested your code and it works, just replace
SPUser user = web.SiteUsers[@"DOMAIN\user"];
with
SPUser user = web.SiteUsers[@"DOMAIN\groupname"];
to add an alert for a group. If you are receiving an error "User cannot be found" make sure you have granted access to the site for the group. Otherwise this error is thrown.

0
 
larrbeCommented:
Hi,
depends what you refer to as group. Check this site http://danishhussain.blogspot.com/2009/07/sending-alerts-to-group-using-share.html about adding groups to alerts in SahrePoint in general. Out of the box you can only use AD email enabled groups but not SharePoint groups.
0
 
dekempeneerAuthor Commented:
It is an AD email enabled group, I can set the alert from within sharepoint itself, but then all users from that group receive an email that they are subscribed to that alert. Using code I can avoid this.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
AlMothanaAlOmariCommented:
use SPGroup instead of SPuser
SPWeb oWebsite = SPContext.Current.Site.RootWeb

{    SPGroup oGroup = oWebsite.SiteGroups["Original_Name"];
   
 }
0
 
dekempeneerAuthor Commented:
That was it, I needed to add this usergroup to the Visitors group.
0
 
beardog1113Commented:
Dear ALL
        could you please let me know how to use this code in sharepoint site? i just created a sharepoint site use HelpDesk template download from Microsoft, and i would like to setup alert, if anyone create a new request, the sharepoint can send me notification email.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.