Thursday, March 5, 2009

SPAlert DynamicRecipient On Custom List

In SharePoint default. alert functionality available. You can subscribe Alert on List or ListItem using AlertMe action. But in certain situation you need to automize this alert. Like Alert automatically send to particular user even though he/she did not subscribe event.

There are two way to do . Create feature that receive ItemAdded or ItemUpdated Event , Find out user and email address , send email using SendEmail Functionality of SharePoint.

Another way is use DynamicRecipient property of SPAlert.

Sample List :

list

Here you can see that To Field is type of Person or Group. Send automatic email to user enter ‘To’ Field.

List Entry look Like (Edit View).

alert list entry

Note : UserName must have email id associated with him.SMTP also configured properly for site.

Now run following code against your site. (use console application).

SPSite site = new SPSite("http://yoursite");
SPWeb web = site.OpenWeb();
SPList lst = web.Lists["Alert Test"];
SPAlert alert = web.Alerts.Add();
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.AlertType = SPAlertType.List;
alert.EventType = SPEventType.All;
alert.DynamicRecipient = "To";
alert.List = lst;
alert.Status = SPAlertStatus.On;
alert.Title = “Auto Email Alert”;
alert.Filter = "<Query><IsNotNull><FieldRef Name='To'></FieldRef></IsNotNull></Query>";            
alert.Update();

You can even use FeatureReceiver to install/ Activate / deactivate this thing as a feature.

6 comments:

Anonymous said...

This is really good.
Thanks,
Can you please explain how to apply AlertTemplate to SPAlert ?

Unknown said...

How do you deactivate the Alert?
I created a feature receiver that toggles off/on. The trouble is, the ID is read only and I cannot grab the alert to delete

web.Alerts.Delete(alert.ID);

dotnetstep said...

I did not delete alert. Instead of that i make its status to off.

SPAlert has one property calledd Status.

SPAlertCollection col = web.Alerts;
for(int i=0;i<col.Count;i++)
{
if(col[i].Title == "Title that you want to match")
{
col[i].Status = SPAlertStatus.Off;
col[i].Update();
}
}
.

On Feature Activated i checked that already alert present with that title . If so i just changed it status to on otherwise i create new alert.

On Feature deactivated i just status to off.

Hope this help you.

I will try to put complete code soon.

Jeff said...

Great article!

Unknown said...

Thank's for article,
I have a problem when I try to call Add() for SPAlert. I seems that SPAlert doesn't have Add() method (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spalert_methods(v=office.15).aspx)

Any suggestions?
Thank you

dotnetstep said...

Hi Sergey Vovnenko ,

Add is not available on SPAlert object.

It is available on SPAlerCollection.

If you look at my example you can see that I have used web.Alerts.Add(). This method return new SPAlert object for that SPWeb object.

Hope this help you.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spalertcollection_methods(v=office.15).aspx