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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.