Ever have one of those Mondays? Well, today was one of those days for me. Apparently, someone (with the first initial P and a last name that starts with S) attempted to copy nearly 400 list items using datasheet view and made a very minor error. Well, more like a pretty serious error - copying the content from 7 cells of the first row to all the 390 other list items. Note to self: Always be sure you are highlighting a cell, not copying it!
Fortunately, the list was versioned, so Eric Bowden and I hurried to write some code to roll back each list item to the previous version. In the hope of saving someone else from having a bad Monday, here's some code that will roll each item of a versioned WSS 3.0 list back to the previous version.
static void Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Usage: ListVersionRestorerConsole [Site] [Web] [ListURL]");
Console.WriteLine("Example: http://extranet.samples.com / http://extranet.samples.com/Lists/VersionedList");
Console.ReadLine();
return;
}
string site = args[0]; //http://extranet.samples.com
string web = args[1]; //"" or "/"
string listURL = args[2]; //@"http://extranet.samples.com/Lists/VersionedList"
Console.WriteLine("This app will revert each list item back one version. CTRL-C to cancel");
Console.WriteLine("Site: " + site);
Console.WriteLine("Web: " + web);
Console.WriteLine("List URL: " + listURL);
Console.WriteLine("Press Enter to continue or CTRL-C to cancel");
Console.ReadLine();
Console.WriteLine("Restoring versions...");
//http://extranet.samples.com/Lists/VersionedList/AllItems.aspx
//SPList oList = new SPSite("http://extranet.samples.com/").OpenWeb().GetList(@"http://extranet.samples.com/Lists/VersionedList");
SPList oList = new SPSite(site).OpenWeb(web).GetList(listURL);
SPListItemCollection collListItems = oList.Items;
foreach (SPListItem oListItem in collListItems)
{
SPListItemVersionCollection collListItemVersions = oListItem.Versions;
if (collListItemVersions.Count > 1)
{
Console.WriteLine(string.Format("\tRestoring {0} version {1} of {2}.", oListItem.Title.ToString(), collListItemVersions[1].VersionLabel, collListItemVersions.ListItem.Title));
collListItemVersions.Restore(1);
}
}
Console.WriteLine("Complete");
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
Have a good week. I will now, thanks to the previous 40 or so lines of code...