Quantcast
Channel: Leonid Esterlis » SharePoint 2010
Viewing all articles
Browse latest Browse all 10

SharePoint 2010 – SharePoint Alerts don't work after upgrading from SharePoint 2007 – Fix This Problem by C# Code

$
0
0

After upgrading to SharePoint 2010 from SharePoint 2007 or moving SharePoint 2010 to another server,


Old SharePoint Alerts stop working and only new created alerts are worked.


I found microsoft support arcticle http://support.microsoft.com/default.aspx?scid=kb;en-us;936759


and one blog for SharePoint 2007 with explanation: http://blogs.msdn.com/b/skelley/archive/2007/06/18/alerts.aspx


I combined two those arcticles in one and build 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.


Enjoy.


 


Viewing all articles
Browse latest Browse all 10

Trending Articles