views:

524

answers:

4

Hi,

I would like to programatically add alert to folders inside a sharepoint list. I have found how to set alerts to a list and this works perfect.

Could someone please help me on how to set alerts to a specific folder which is inside a list.

Below is the code i currently have which sets alerts only to the list.

    using (SPSite site = new SPSite("http://site/"))
{
using (SPWeb web = site.OpenWeb())
{
    SPUser user = web.SiteUsers["domain\\user"];
SPAlert newAlert = user.Alerts.Add();
newAlert.AlertType = SPAlertType.List;
newAlert.List = web.Lists["Documents"];
newAlert.EventType = SPEventType.All;
newAlert.AlertFrequency = SPAlertFrequency.Immediate;
//passing true to Update method will send alert confirmation mail
newAlert.Update(true);
}
}

Your help will be much appreciated

THIS QUESTION IS RESOLVED! PLEASE SEE MY POST BELOW WITH THE LINK - SEE - LINK

A: 

That's not possible out of the box, but after googling I found an interesting possibility though, check out Mike Walsh's answer on this post, it entails creating a view in the folder and then attaching the alert to that view.

Colin
A: 

You need to update the line with

newAlert.List = web.Lists["Documents"];

With

SPFolder fldr = web.GetFolder("/ListName/FolderName");
newAlert.Item=fldr.Item;

Also note that Folder is also another item.

Kusek
Hi Kusek Thank you for the answer. I tried as you mentioned but it is throwing an error:Microsoft.SharePoint.SPException: The object specified does not belong to a list. at Microsoft.SharePoint.SPWeb.GetItem(String strUrl, Boolean bFile, Boolean cacheRowsetAndId) at Microsoft.SharePoint.SPFolder.get_Item() at Alerts_Programatically.Program.Main(String[] args) in C:\Inetpub\wwwroot\........\Program.cs:line 23YOUR HELP WILL BE MUCH APPRECIATED! THANK YOU
Mo
i tried fixing and managed to get rid of the above error but now getting this one: any ideas please?Microsoft.SharePoint.SPException: Item property cannot be set for this type of alert. at Microsoft.SharePoint.SPAlert.set_Item(SPListItem value) at Alerts_Programatically.Program.Main(String[] args) in C:\Inetpub\wwwroot\......\Program.cs:line 23
Mo
+1  A: 

This question has been answered very nicely by Steve Curran

Click Here to view the solution

Mo
A: 

Hi all,

I developed the following code to add a notification to a user but it doesn't work for folders. Have you ever had problems with alerting a folder? The code doesn't give me any error, simply it doesn't alert when a change occure. It works properly if I create an alert to the whole list.

Thanks in advance!!

SPAlert alert = user.Alerts.Add();

alert.AlertType = SPAlertType.Item; alert.Item = item; alert.EventType = evenType; alert.AlertFrequency = SPAlertFrequency.Immediate;

alert.Title = title; alert.AlertTemplate = list.AlertTemplate; alert.Filter = String.Format(String.Empty); alert.Update(true); alert.AlwaysNotify = true; user.Update();

Dario Zanelli