rilpoint_mw113

Fix Alerts

Source: Link

Use this console application based on 64bit Target inside VS2010.

static void Main(string[] args)
        {
            string strNewSiteCollection = string.Empty;
            string strOldSiteCollection = string.Empty;
            // First (and only) argument is the URL to the Site Collection
            if (args.Length > 1)
            {
                strNewSiteCollection = args[0];
                strOldSiteCollection = args[1];
                try
                {
                    using (SPSite spsitecol = new SPSite(strNewSiteCollection))
                    {
                        using (SPWeb sprootweb = spsitecol.RootWeb)
                        {
                            ListAndFixAlerts(sprootweb, strOldSiteCollection, strNewSiteCollection);
                        } // using sprootweb
                    } // using spsitecol
                }
                catch (System.IO.FileNotFoundException ioex)
                {
                    Console.WriteLine("Could not access Site Collection: {0}", ioex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An unexpected error occurred: {0}", ex.Message);
                } // try
            }
            else
                Console.WriteLine("No Site Collection URL was specified.");
        }
 
        //Enumerates all Alerts present on a Site Collection and modifies them
        //so that wrong information (for example, in the SiteUrl column) is corrected.
        static void ListAndFixAlerts(SPWeb spweb, string oldurl, string newurl)
        {
            Console.WriteLine("\n{0} Alert(s) for site: {1} ({2})", spweb.Alerts.Count, spweb.Title, spweb.Url);
 
            foreach (SPAlert alert in spweb.Alerts)
            {
                try
                {
                    Console.WriteLine(" {2} Alert \"{0}\" for user \"{1}\" \n  (Id: {3})\n", alert.Title, (alert.User != null ? alert.User.Name : "<unknown>"), alert.AlertFrequency.ToString(), alert.ID.ToString());
 
                    //Preserve the initial Alert frequency (Immed., Daily, Weekly).
                    SPAlertFrequency afPrevious = alert.AlertFrequency;
 
                    //Change the Alert frequency so the datarow inside
                    //is touched by SharePoint API.
                    if (afPrevious == SPAlertFrequency.Immediate)
                        alert.AlertFrequency = SPAlertFrequency.Weekly;
                    else
                        alert.AlertFrequency = SPAlertFrequency.Immediate;
 
                    try
                    {
 
                        SPPropertyBag spprop = alert.Properties; 
                        if (spprop["siteurl"] != null)  //fix hard coded properties
                        {    
                            Console.WriteLine("pre alert site url: " + spprop["siteurl"].ToString());    
                            if(spprop["siteurl"].Contains(oldurl))  //found old link reference/   
                            {        
                                spprop["siteurl"] = spprop["siteurl"].Replace(oldurl, newurl);       
                                //Console.WriteLine("post alert site url: " + spprop["siteurl"].ToString());        
                                Console.WriteLine("Updated siteurl in properties");
                                spprop.Update();    
                            }
                        }
 
                        if (spprop["mobileurl"] != null)
                        {    
                            Console.WriteLine("alert mobile url: " + spprop["mobileurl"].ToString());    
 
                            if (spprop["mobileurl"].Contains(oldurl))  //found old link reference/   
                            {        
                                spprop["mobileurl"] = spprop["mobileurl"].Replace(oldurl, newurl);        
                                Console.WriteLine("Updated mobileurl in properties");        
                                spprop.Update();    
                            }
                        } 
 
                        alert.Update();
 
                        //Change the Alert frequency back to the initial state.
                        alert.AlertFrequency = afPrevious;
                        alert.Update();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(" -> Error changing Alert. {0}", ex.Message);
                    } // inner try
                }
                catch (Exception ex)
                {
                    Console.WriteLine(" An unexpected error occurred while accessing the Alert object: {0}", ex.Message);
                } // outer try
            } // foreach alert
 
            //Handle all Sub-Webs recursively.
            if (spweb.Webs.Count > 0)
            {
                for(int i = spweb.Webs.Count -1; i >= 0; i--)
                {
                    using(SPWeb spsubweb = spweb.Webs[i])
                    {
                        ListAndFixAlerts(spsubweb, oldurl, newurl);
                    } // using spsubweb
                } //for subwebs
            } //if has subweb
        }

You can download binary executable program here: UpdateAlerts.zip

You must run this program inside command line prompt by using this syntax:

UpdateAlerts.exe http://newSiteCollection http://oldSiteCollection

After running this program your sharepoint alerts will be started to work.

Skin by RIL Partner