Add Alerts to user at the sub area level

Add alerts at the sub area level is slightly different than adding alerts at the WSS sites. At the sub area level, the SPAlertCollection object cannot be used. We should use the AlertCollection object. If you use SPAlertCollection object at the sub area level, you will end up with "Access Denied" error message

Here you go for the sample code.

   //Get the reference for the topology manager
TopologyManager tm = new TopologyManager();
//Get the portal site.
PortalSite ps = tm.PortalSites[ new Uri("http://karthick:6333") ];
//Get the portal site context
Microsoft.SharePoint.Portal.PortalContext ctx = Microsoft.SharePoint.Portal.PortalApplication.GetContext(ps);
//Get the home area
Area home = AreaManager.GetArea(ctx, AreaManager.GetSystemAreaGuid(ctx, SystemArea.Home));
//Get the area where we want to add alerts
Area d = home.Areas["<AreaName>"];

   //initialize user profile config manager object
UserProfileManager upm = new UserProfileManager(ctx);

//Get the user profile
UserProfile u = upm.GetUserProfile("<Domain\\UserName>");
//Create the Alert at the area level.
Microsoft.SharePoint.Portal.Alerts.Alert alert = u.Alerts.CreateAlert("Area");

//Set the properties for the alert.
//Alert name
alert.Name = "MyNewAlert";
//Area name
alert.ObjectDisplayName = "<AreaName>";
//Area id
alert.ObjectID = d.ID.ToString();
//Area url
alert.ObjectUrl = d.WebUrl;
//Query string with area id and events name
alert.Query = "(((\"urn:schemas-microsoft-com:publishing:Category\" = '270a9272-71ec-45ee-821c-1b990592205b') OR (\"urn:schemas-microsoft-com:sharepoint:portal:parentid\" = '270a9272-71ec-45ee-821c-1b990592205b')) AND ((\"PKMEvent\"='ADD') OR (\"PKMEvent\"='MODIFY')))";

//Specify the delivery channel
alert.DeliveryChannels.Add(new Microsoft.SharePoint.Portal.Alerts.PortalChannelSettings());
//commit the changes
//Activate the alert
MessageBox.Show("Alert Added");


Note : The area guid has been hard coded. It should be changed accordingly.