Thursday, April 24, 2014

Site backup and restore programmatically - SharePoint 2013

Site backup and restore programmatically in SharePoint 2013 can be simple just like that in two lines of code.
To run this code successfully you must have the permission on the SharePoint farm and site collection administration.

Create the Shared folder on your server and set the permission to "Everyone" on the folder.

SPSecurity.RunWithElevatedPrivileges(delegate()
 {
 SPWebApplication webApps = SPWebApplication.Lookup(new Uri("http://webapplicationUrl/"));
SPSiteCollection sites = webApps.Sites;
 sites.Backup(SPContext.Current.Site.Url, @"\\Server\\SiteBackUp\\backup.dat", false);
 });
After this ensure your backup file work as expected by restoring the site using powershell command Restore-SPSite -Identity http:/site/sites/restoreSite -Path d:\backup.dat -DatabaseServer database serverName -DatabaseName WSS_Content -Force Alternatively you can also use Restore method of SPSiteCollection method. SPSiteCollection Class