Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

SharePoint 2010 : The SharePoint Object Model (part 3) - Programmatically Using SQL Snapshots

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
5/24/2011 11:47:36 AM

Programmatically Using SQL Snapshots

The SPDatabase type in the Microsoft.SharePoint.Administration namespace represents a SQL Server database, and SharePoint uses SPDatabase and its derived types to read, write, and manipulate the contents of databases that are used by the farm. Particularly noteworthy among the SPDatabase-derived types is the SPContentDatabase type, which represents a content database housing site collections.

In SharePoint 2010, the SPDatabase type has been extended with a Snapshots property. As shown in Figure 4, the Snapshots property exposes a collection of type SPDatabaseSnapshotCollection. Through the Snapshots collection and each of its SPDatabaseSnapshot items, it is possible to create, delete, and manage SQL Server snapshots for the database in the underlying SQL Server instance.

Figure 4. Database snapshot support for SPDatabase and derived types.

When a snapshot is created, either programmatically through the SharePoint object model or directly through SQL Server, that snapshot is treated as if it were a completely different readonly database on SQL Server. Although snapshots are commonly created from live SharePoint content databases, they are not attached to the SharePoint farm. Because the snapshots aren’t affiliated with a SharePoint farm, you need to use the new unattached content database model (through the SPContentDatabase.CreateUnattachedContentDatabase method) to interact with them.

Listing 4 demonstrates the same site collection backup operation that was shown in Listing 3, but it shows how you can use a database snapshot to enhance the overall backup process. By executing the SPSiteCollection.Backup operation against a database snapshot instead of the live content database, there is no need to lock the live database while the backup is being performed. Users can continue to conduct read and write operations as they normally would, and the backup operation can proceed against the snapshot without worries of corruption or inconsistencies. When the backup operation is complete, the snapshot is deleted to free any resources it held on SQL Server.

Note

The code shown in Listing 4 assumes that the Microsoft.SharePoint.dll assembly is referenced and that the Microsoft.SharePoint, Microsoft.Share-Point.Administration, and Microsoft.SharePoint.Administration.Backup namespaces have been imported for use by the ExecuteSiteCollectionBackupWithSnapshot method. In addition, you should configure the Visual Studio project containing the ExecuteSiteCollectionBackupWithSnapshot method to target the .NET Framework 3.5 on an x64 platform.


Listing 4.
public static void ExecuteSiteCollectionBackupWithSnapshot()
{
// As with the ExecuteSiteCollectionBackup method, these variable values
// would normally be supplied to the method rather than set here.
String backupFilename = @"E:\Backup\SampleSiteBackup.bak";
String siteCollectionUrl = @"http://spdev:18380";
Boolean overwriteIfExisting = true;

// These two variables are set with properties from the SPSite object that
// is created. To minimize the size of the using block that follows, the
// variables are scoped here.
SPContentDatabase housingDb;
String rootRelativeUrl;

// Obtain the critical property values that are needed from the SPSite.
using (SPSite siteToBackup = new SPSite(siteCollectionUrl))
{
// The content database that houses the site collection is needed to
// generate a snapshot. The relative site collection URL
// is needed for the eventual unattached DB backup operation.
housingDb = siteToBackup.ContentDatabase;
rootRelativeUrl = siteToBackup.ServerRelativeUrl;
}

// Only Developer and Enterprise versions of SQL Server support snapshots. If
// snapshots aren't supported, the process can't continue.
if (!housingDb.Snapshots.IsSnapshotSupported)
{
throw new NotSupportedException("Snapshots not supported.");
}

// Each SPDatabase-derived type (including SPContentDatabase) has a Snapshots
// collection that serves as the gateway to working with snapshots. Refresh
// the collection and then use it to create a new snapshot.
SPDatabaseSnapshotCollection allSnapshots = housingDb.Snapshots;
allSnapshots.RefreshSnapshots();
SPDatabaseSnapshot hostingDbSnapshot = housingDb.Snapshots.CreateSnapshot();

// Once SQL Server creates the snapshot, it's just like any other database.
// SharePoint's unattached content database functionality is used to attach to
// the snapshot as if it were a read-only content database.
SPContentDatabase snapshotDb = SPContentDatabase.CreateUnattachedContentDatabase
(hostingDbSnapshot.ConnectionString);

// With a valid SPContentDatabase reference, the collection of site collections
// in the snapshot can be referenced and used to perform the site collection backup.
SPSiteCollection sitesInSnapshot = snapshotDb.Sites;
sitesInSnapshot.Backup(rootRelativeUrl, backupFilename, overwriteIfExisting);

// When the backup operation is over, delete the snapshot to instruct SQL Server
// to release the resources associated with it.
hostingDbSnapshot.Delete();
}



A parting word of caution regarding the use of snapshots is warranted. Listing 4 includes a call to the RefreshSnapshots method to ensure that the collection of SPDatabaseSnaphot objects is current prior to any collection manipulation activities. This is done because there are regular processes within SharePoint Foundation, such as the Microsoft SharePoint Foundation Snapshot Management timer job, that can create and delete database snapshots. Any SPDatabaseSnapshotCollectionEnabledManagement property value of true is subject to regular snapshot maintenance by SharePoint, and this that has an maintenance can lead to the addition and deletion of snapshots during the execution of your code. For this reason, it is prudent to refresh the contents of the collection through the RefreshSnapshots method before attempting any manipulation of the collection within your code.

Other -----------------
- SharePoint 2010 : The SharePoint Object Model (part 2) - Export, Import, and Associated Types & Site Collection Backup and Restore
- BizTalk 2010 Recipes : Business Activity Monitoring - Setting Up BAM Alerts
- BizTalk 2010 Recipes : Business Activity Monitoring - Using the BAM Portal
- Exchange Server 2010 : Ensuring Message Integrity (part 3) - Configuring Permissions on Active Directory Objects & Rights Management Services Federation
- Exchange Server 2010 : Ensuring Message Integrity (part 2) - Using TLS and MTLS & Implementing Domain Security
- Exchange Server 2010 : Ensuring Message Integrity (part 1) - Using S/MIME Extensions
- Windows Server 2003 : Designing a Security Infrastructure - Securing a Wireless Network
- Windows Server 2003 : Designing a Security Infrastructure - Planning a Security Update Infrastructure
- Windows Server 2008 : Network Addressing (part 3) - IPv4 to IPv6 Transitional Techniques
- Windows Server 2008 : Network Addressing (part 2) - Addressing IPv6
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server