The following code snippet can be used to programmatically backup the site collection.
static void Main(string[] args)
{
var backupSiteUrl = "SiteUrl";
var backupfilename = @"D:\\Backup\\SharePointSiteBackup_.bak";
SiteBackup(backupSiteUrl, backupfilename);
}
The following code snippet can be used to programmatically restore the site collection.
static void Main(string[] args)
{
var restoreSiteUrl = "SiteUrl";
var restoreBackupFile = "D:\\Backup\\SharePointSiteBackup_24-02-2014.bak";
RestoreSite(restoreSiteUrl, restoreBackupFile);
}
private static void RestoreSite(string siteUrl, string fileName)
{
try
{
SPWebApplication webApplication = null;
webApplication = SPWebApplication.Lookup(new Uri(siteUrl));
SPSiteCollection sitecols = webApplication.Sites;
sitecols.Restore("Teamsite", fileName, true);// overwrite sitecollection if already exists
Console.WriteLine("Site collection restored successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
var backupSiteUrl = "SiteUrl";
var backupfilename = @"D:\\Backup\\SharePointSiteBackup_.bak";
SiteBackup(backupSiteUrl, backupfilename);
}
public static void SiteBackup(string siteUrl, string fileName)
{
try
{
SPWebApplication webApplication = null;
webApplication = SPWebApplication.Lookup(new Uri(siteUrl));
SPSiteCollection sitecols = webApplication.Sites;
string backupName = fileName;
sitecols.Backup(siteUrl, backupName, true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
The following code snippet can be used to programmatically restore the site collection.
static void Main(string[] args)
{
var restoreSiteUrl = "SiteUrl";
var restoreBackupFile = "D:\\Backup\\SharePointSiteBackup_24-02-2014.bak";
RestoreSite(restoreSiteUrl, restoreBackupFile);
}
private static void RestoreSite(string siteUrl, string fileName)
{
try
{
SPWebApplication webApplication = null;
webApplication = SPWebApplication.Lookup(new Uri(siteUrl));
SPSiteCollection sitecols = webApplication.Sites;
sitecols.Restore("Teamsite", fileName, true);// overwrite sitecollection if already exists
Console.WriteLine("Site collection restored successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Thanks your post.
ReplyDelete