At some point or another in your SharePoint dev work, you'll probably have to create a backup or restore a backup of a Site Collection.  Typically you'll use the CA (I hope you won't cause it's a pain!), if your smart, you'll use the STSADM commands to backup or restore your site collection.  But what if you need to create a custom web part to do a backup and restore, or better yet, what if you want to make your own web page so that you can streamline the backup/restore of a site collection?  Yet again, the API comes to the rescue and lets you do both a backup and restore through code.  The SPSiteCollection object has the commands you'll want to use, "Backup" and "Restore".  Below is a quick code snippet to get you started.

SPWebApplication parentWebApp;
 
using(SPSite siteCol = new SPSite("http://{url}"))
{
   parentWebApp = siteCol.WebApplication;
}
 
//parentWebApp.Sites.Backup("http://{url}",@"c:\site_collection_backup.bak",true);
parentWebApp.Sites.Restore("http://{url}",@"c:\site_collection_backup.bak",true);

What I did with the code above was grab a site collection that I have so that I can get the parent web application.  Once I have the parent web application, I can then use its Sites SPSiteCollection property to give me all the sites.  I can then use the backup or restore of a site collection to a specfic url using a specific backup.


Posted in:   Tags:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017 Tony Testa's World