sharepointcsomalerts

How to disable SharePoint 2013 alerts using CSOM


Using CSOM, I am able to retrieve the collection of alerts for a SharePoint Online site and disable each alert using the following code:

                    context.Load(context.Web.Alerts,
                        collection => collection.Include(
                            alert => alert.ID,
                            alert => alert.Status));
                    context.ExecuteQueryWithRetry();

                    foreach (SP.Alert alert in context.Web.Alerts)
                    {
                        if (alert.Status == AlertStatus.On)
                        {
                            context.Load(alert);
                            alert.Status = AlertStatus.Off;
                            alert.UpdateAlert();
                        }
                    }
                    context.ExecuteQueryWithRetry();

This doesn't work for SharePoint 2013 though. I get the error: "Field or property 'Alerts' does not exist". Can this be done with CSOM?


Solution

  • Same question as How to disable SharePoint 2013 alerts using CSOM

    SharePoint 2013 On-Premise CSOM Web object didn't support Alerts property.

    Please use SharePoint Server Object Model below instead:

            using (SPSite site=new SPSite("http://sp/sites/MyDev"))
            {
                SPWeb web = site.OpenWeb();
                SPAlertCollection alerts = web.Alerts;
                foreach (SPAlert alert in alerts)
                {
                    alert.Status = SPAlertStatus.Off;
                    alert.Update();
                    web.Update();
                }
            }