Thursday, 19 June 2014

Alerts update fixes post SharePoint Migration

Based on the contributions from Kirk Stark & skelley. There were few queries for Q & A section of getting the siteUrl property to be updated by Site Collection against the Web Application URL.

First query your site collections DB migrated based on DB Attach Upgrade against the following tables immedsubscriptions & schedsubscriptions
That's what I modified the above codes from powershell  & console app to take inputs from a text file as 3 set of values.
It reads a Alerts.csv placed at C:\ drive which has parameters as
SiteUrl,OldUrl,NewUrl

values can be
SiteCollection1Url,OldUrl,NewUrl
SiteCollection1Url,Old1Url,New1Url
SiteCollection2Url,OldUrl,NewUrl
SiteCollection2Url,Old1Url,New1Url
based on the results retuned against the above table in your case. (based on different zones values default, Intranet or Internet)

Note that the SiteUrl in the properties section has to be Web Application url while Mobile url may not the same. Please note the mobileurl section has been commented out. To cater to that scenario, u can pass 5 set of parameters in Alerts.csv and update accordingly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.IO;

namespace AlertUpdates
{
   class Program
{
static int countsiteurlprop = 0;
static int countmobileurlprop = 0;

static string strSiteCollection =string.Empty;// Site collection url to open new context

static string strOldString =string.Empty; //old url or portion of string which needs to be replaced.// http://novahome

static string strNewString = string.Empty; //new url which will be replaced by the old Url

static void Main(string[] args)
{
try
{
using (StreamReader reader = new StreamReader(File.OpenRead(@"C:\Alerts.csv")))


{
while (!reader.EndOfStream)


{
string line = reader.ReadLine();

var values = line.Split(',');


strSiteCollection = values[0];

strOldString = values[1];

strNewString = values[2];
// GetFileOutput();


IterateSites();

}

}

}
catch (Exception ex)
{
 
Console.WriteLine("Error:"+ex.Message);

}
 Console.ReadLine();

}
        
           
static void IterateSites()


{
           

try


{
using (SPSite spsitecol = new SPSite(strSiteCollection))


{
using (SPWeb sprootweb = spsitecol.RootWeb)


{

ListAndFixAlerts(sprootweb);
} // 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(" Modified count for siteUrlCount : {0} & mobileUrlCount: {1}", countsiteurlprop, countmobileurlprop);

Console.WriteLine(Environment.NewLine + "Update done! Click to exit.");

Console.ReadLine();


}
//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)


{
Console.WriteLine("\n{0} Alert(s) for site: {1} ({2})", spweb.Alerts.Count, spweb.Title, spweb.Url);

string oldurl = strOldString;

string newurl = strNewString;// spweb.Site.Url;

foreach (SPAlert alert in spweb.Alerts)


{
try


{


SPPropertyBag spprop = alert.Properties;

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


{
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");


countsiteurlprop++;

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");


countmobileurlprop++;

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);
} // using spsubweb

} //for subwebs

} //if has subweb


}

}

}