How to revert library items to its previous working condition.

There are some scenarios in day to day development, where current changes need to revert to  its previous working condition.
This can be achieved by finding the correct Version from Version-History of the item
and restore it accordingly.
The below code snippet is just for information, for files in document library and has the same approach for List Item as well.

Sample code Snippet…..

using (SPSite mksSite = new SPSite(“http://devsp:9900/”))
{
using (SPWeb mksWeb = mksSite.OpenWeb())
{
mksWeb.AllowUnsafeUpdates = true;
SPList lst = mksWeb.Lists[“helloListName”];
IEnumerable<SPListItem> demoLib = lst.Items.OfType<SPListItem>();
SPListItemCollection demoFormCollection = lst.Items;
XmlDocument itemVersionDoc = new XmlDocument();
using (WebClient client = new WebClient())
{
for (int i = 0; i < demoFormCollection.Count; i++)
{
SPListItem item = demoFormCollection[i];
byte[] fileByte = item.File.OpenBinary(SPOpenBinaryOptions.None);

// Validate your output if this is the correct one. THEN no further action required//

if (THE OUTPUT AFTER VALIDATION IS INCORRECT THEN)
{

IEnumerable<SPFileVersion> listItems = item.File.Versions.OfType<SPFileVersion>();

var allResults = listItems.OrderByDescending(prod => prod.ID);

foreach (var vitem in allResults)
{

bool needToFindOtherVersion = true;
{
if (needToFindOtherVersion)
{
//<MOST IMPORTANT> “_vti_history” IS THE PLACE FOR ALL THE VERSIONS//

client.Credentials = CredentialCache.DefaultCredentials;
fileByte = client.DownloadData(mksWeb.Url.TrimEnd(‘/’)
+ “/_vti_history/” + vitem.ID.ToString() + “/helloListName/” + item.Title);

//NOTE: fileByte is the OUTPUT from  VERSION HISTORY//

//CHECK IF THE OUTPUT IS THE CORRECT ONE AS PER REQUIREMENT//
if (THE OUTPUT AFTER VALIDATION IS INCORRECT THEN)
{
needToFindOtherVersion = true;
}
else
{
needToFindOtherVersion = false;

item.File.Versions.RestoreByLabel(vitem.VersionLabel);
Console.WriteLine(“Correct Version Restored Successfully.“);
break;
}
}

}
}

}
mksWeb.AllowUnsafeUpdates = false;
}

Export Import Page

Export Page

One problem with STSADM tool is it cannot export List Item (like Publishing Pages…)  , It’s better to do export using object model and then import in production environment.

Options to export are :-
SPDeploymentObjectType.File              
SPDeploymentObjectType.Invalid
SPDeploymentObjectType.Folder
SPDeploymentObjectType.List
SPDeploymentObjectType.ListItem;
SPDeploymentObjectType.Site
SPDeploymentObjectType.Web

SPSite site = new SPSite(SiteName);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[“Pages”];
SPExportObject exportObject = new SPExportObject();
SPListItem listItem = null;
listItem = list.Items[i];
exportObject.Id = listItem.UniqueId;
exportObject.Type =SPDeploymentObjectType.ListItem;

SPExportSettings exportSettings = new SPExportSettings();
SPExportSettings settings = new SPExportSettings();
exportSettings.SiteUrl = SiteName;
exportSettings.FileLocation =@”E:\WorkArea\XML_XSL\“;
exportSettings.BaseFileName =”SITE1.cmp”;
exportSettings.ExportMethod =SPExportMethodType.ExportAll; 

exportSettings.AutoGenerateDataFileName =true;
exportSettings.ExportObjects.Add(exportObject)

exportSettings.IncludeSecurity =SPIncludeSecurity.All;
exportSettings.IncludeVersions =SPIncludeVersions.All;
SPExport export = new SPExport(exportSettings);
export.Run();
string exportFileLocation = exportSettings.FileLocation + “\\” + exportSettings.BaseFileName;
 
Import Page

string targetListURL = “http://devsp:5001/Pages/“;
SPImportSettings settings = new SPImportSettings();
settings.SiteUrl = targetListURL;
settings.BaseFileName = “SITE1.cmp”;
settings.FileLocation = Path;
settings.FileCompression = true;
settings.IgnoreWebParts = false;
settings.IncludeSecurity = SPIncludeSecurity.All;
settings.RetainObjectIdentity = false;
settings.UpdateVersions = SPUpdateVersions.Append;
settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
settings.WebUrl = targetListURL;
settings.CommandLineVerbose = true;
settings.LogFilePath = @”E:\WorkArea\XML_XSL\bkp\import.log”;settings.Validate();
SPImport import = new SPImport(settings);

import.Run();

SharePoint 2010 Backup, Recovery, Availability, and Disaster Recovery

SharePoint Server 2010 provides two backup systems:
1. Farm
2.Granular

Farm Backup

The entire server farm is backed up, including settings from the configuration database.

It supports both Full and differential backup.

The farm backup architecture in SharePoint Server 2010 starts a
Microsoft SQL Server backup of
1. Content DB
2. Service application DB
3. Writes configuration content to files
4. Search index files

A Web application backup includes:
Application pool name and application pool account
Authentication settings
General Web application settings such as alerts and managed paths
Internet Information Services (IIS) binding information, such as the protocol type, host header, and port number
Changes to the Web.config file
(that have been made through the object model or Central Administration)
Sandboxed solutions

Search service application backup

SharePoint Server 2010 starts a SQL Server backup of the Search administration database, crawl databases, and property databases, and also backs up the index partition files in parallel.

he backup process is as follows:

  1. Master merges are paused to preserve the master index.
  2. A full database backup starts.
  3. The master index is backed up.
  4. Crawls are paused. The pause in crawling is much shorter than during a backup of Microsoft Office SharePoint Server 2007 search, and does not last the full duration of the backup process.
  5. All shadow indexes are backed up.
  6. An incremental database backup starts.
  7. Crawls are resumed.
  8. Master merges are resumed.

More detail can be found here.