2

So here's my Problem:

I have a list which is used to notificate user. (Announcements) So it's essential that the email notifications work for most of the time. The announcement (items) get saved in folders, so that only specific groups get informed and can read these announcements. So far so good.

The problem is, when I add new folders programmatically, which happens occasionally, the email notification gets send out, too. I don't wish that to happen, so is there any way to stop these notifications while I create the folder programmatically within an EventReceiver? (ItemAdded on another list)

I have found some ways to do this manually, but not within my code. (And not to forget, I need to change things back later on) I tried the SystemUpdate on the item instead of the the normal Update-method and some other things. Event-firing is disabled of course, too, but nothing works.

P.S: It's a SharePoint Server 2010 Environment.

3 Answers 3

3

I'm assuming you have users setting up their own alerts, so you can't really handle this as the alerts are created, but you can change the behavior of existing alerts programatically.

I would recommend iterating through SPWeb.Alerts, check each SPAlert object to see if it's scoped to your list (alert.AlertType == SPAlertType.List && alert.ListID == YourListID), and then setting the Filter property to a query that would filter out Folder items. Try something like this (...didn't actually test, so YMMV):

    SPList list = SPContext.Current.Web.Lists.TryGetList("My List");
    foreach (SPAlert alert in SPContext.Current.Web.Alerts)
    {
        if (alert.AlertType == SPAlertType.List && alert.ListID == list.ID)
        {
            alert.Filter = "<Query><Neq><FieldRef Name=\"ContentType\" /><Value Type=\"Computed\">Folder</Value></Neq></Query>";
            alert.Update();
        }
    }

I don't think I would recommend doing the turn on/turn off of this every time...turn it on once and leave it on...you could also check to see if the filter is there first before applying it and updating the SPAlert.

Hope this helps.

UPDATE: I had incorrectly set Type to Integer instead of Computed. With it set to Computed the Filter works.

5
  • Thanks, good anwser, but it unfortunally didn't worked for me that way. (Maybe some kind of syntax-error in the CAML, I couldn't figure it out) However, I created a method to simply enable/disable these alerts, based on your answer. Commented Aug 22, 2011 at 12:00
  • Like I said, didn't test it...if you had changed Type="Integer" to Type="Computed" it would have worked. Sorry I missed that when typing it.
    – Rob D'Oria
    Commented Aug 22, 2011 at 12:24
  • I tried Type="Text"... Well, no problem anyway. I will remember your answer, but for this projekt, the on/off-switch method is a little more useful. Commented Aug 22, 2011 at 12:33
  • Since it's monday and after 3pm (in my time zone) I took myself some minutes to test your code again with >Type="Computed"<, and it works like a charm. You should update your answer, in case someone else has this problem. (People are so lazy about reading comments, these days) Commented Aug 22, 2011 at 13:50
  • Thanks for trying it out...answer updated per your suggestion.
    – Rob D'Oria
    Commented Aug 22, 2011 at 13:54
2

I managed to find an answer myself, well almost atleast.

Based on Rob D'Ora's answer (which didn't worked, even though it looked like the right thing) I created a method to turn alerts on/off. (Maybe not the most efficient way, but it works)

 public static void ToggleAlertStatus(SPWeb web, Guid listID,
     SPAlertType alertType, SPAlertStatus alertStatus)
 {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
         using (SPWeb Web = new SPSite(web.Site.ID).OpenWeb(web.ID))
         {
             foreach (SPAlert alert in web.Alerts)
             {
                 if (alert.AlertType == alertType
                     && alert.ListID == listID
                     && alert.Status != alertStatus)
                 { // change the status if the current status is different,
                     // alert is set on the right list,
                     // and the alerttype is right, too.
                     web.AllowUnsafeUpdates = true;
                     alert.Status = alertStatus;
                     alert.Update();
                     web.AllowUnsafeUpdates = false;
                 }
             }
         }
     });
 }

I know I played a bit safe with "AllowUnsafeUpdates" and the "RunWithElevatedPrivileges", but If I learned one thing (especially) with SharePoint than it's that playing safe is always a good thing.

0

Wouldn't you just need to put some logic in your email function to check the content type of the item? If it's of Folder content type, do nothing, if it's Announcement, send the email.

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.