Pergunta

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.

Foi útil?

Solução 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.

Outras dicas

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.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top