0

I'm attempting to create an alert via code such that it triggers only when a document is published. I have used the following with no success.

SPAlert

alert = currUser.Alerts.Add();
alert.Filter ="<Query><Eq><FieldRef Name=\"vti_level\"/><Value type=\"integer\">1</Value></Eq></Query>"; 
alert.Title = "Test Alert '" + selectedFileName + "'" ; 
alert.AlertType = SPAlertType.Item; 
alert.EventType = SPEventType.All; 
alert.Item = selectedFileListItem;
alert.AlertFrequency = SPAlertFrequency.Immediate; 
alert.AlwaysNotify = true;  
alert.Update(true);
2
  • Betty, you're not going to be able to setup an alert to fire when a document is specifically published. You could setup an alert to fire on modify, which should pick-up the publishing action, but it will pick-up any action that modifies the document.
    – Rob D'Oria
    Commented Jul 25, 2011 at 14:29
  • You wouldn't want the scope of the alert to be a specific item, you'd want to target a list...so alert.List (or alert.ListId) instead of alert.Item.
    – Rob D'Oria
    Commented Jul 25, 2011 at 14:57

2 Answers 2

4

Betty, try something like this (...it works for me):

            SPAlert alert = site.RootWeb.CurrentUser.Alerts.Add();
            alert.Filter = "<Query><Eq><FieldRef Name=\"_Level\" /><Value Type=\"Integer\">1</Value></Eq></Query>";
            alert.Title = "Test Publishing Alert";
            alert.AlertType = SPAlertType.List;
            alert.EventType = SPEventType.Modify;
            alert.List = site.RootWeb.Lists.TryGetList("Site Pages");
            alert.AlertFrequency = SPAlertFrequency.Immediate;
            alert.AlwaysNotify = true;
            alert.Update();

Note that I'm using _Level instead of vti_level, alert.List instead of alert.Item, and SPEventType.Modify instead of SPEventType.All. You also have Value type="integer" in your query, s/b Value Type="Integer" ...integer is probably OK, but type definitely won't work.

2
  • Rob, thank you for your help. Would your code fire for ANY file that is published in this list? I specifically need it to fire for each specific file.
    – Betty
    Commented Aug 5, 2011 at 17:11
  • 1
    Yes...it would work for any file. Just as a FYI, you should mark my answer as correct in lieu of creating your own answer and marking it correct...I don't get any credit for answering your question when you do that.
    – Rob D'Oria
    Commented Aug 5, 2011 at 17:50
0

Thanks to Rob I was able to solve this problem. The following worked for me.

            SPAlert alert = sourceWebObj.CurrentUser.Alerts.Add(); 
            alert.Filter = "<Query><Eq><FieldRef Name=\"_Level\" /><Value Type=\"Integer\">1</Value></Eq></Query>";
            alert.Title = selectedFileName + " - Publish Alert";                 
            alert.AlertType = SPAlertType.Item;      
            alert.EventType = SPEventType.Modify;                
            alert.Item = selectedFileListItem;
            alert.AlertFrequency = SPAlertFrequency.Immediate; 
            alert.AlwaysNotify = true; 
            alert.Update();
1
  • I tried exact code it doent seem to work, will this work only for Item or List as well .
    – Kusek
    Commented Aug 19, 2011 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.