Verify the Read-Only status for databases\site collection programmatically sharepoint 2010

Read – only databases are useful when you want to perform maintenance or provide copies of your database globally but you don ’ t want to allow users
to write to the database. Having a read – only database can work with high – availability technologies, such as log shipping or database snapshots. You can also use the object model to detect whether the site and database are in a read – only state, so that you can either continue to try to write to the database or wait for the read -
only state to be removed. The Boolean ReadOnly properties on the SPSite and the SPDatabase objects make it easy to check the read – only state of your site collection and your database. There is also a Boolean ReadLocked property you can use to see if the site collection is also read – locked so that you can ’ t retrieve data from it. The code that follows shows how to use these properties.

SPDatabaseServiceInstance databaseServiceInstance = new SPDatabaseServiceInstance();
SPService service = SPFarm.Local.Services.GetValue < SPDatabaseService > ();
foreach (SPDatabaseServiceInstance si in service.Instances)
{
if (si.TypeName == “Microsoft SharePoint Foundation Database” || si.TypeName.Contains(“Microsoft SharePoint Foundation”))
{
databaseServiceInstance = si;
break;
}}
SPDatabase database = databaseServiceInstance.Databases["WSS_Content"];
//Check to see if the database is read only
bool isReadOnly = database.IsReadOnly;
//Check to see if the site is read only
SPSite site = new SPSite(“http://SPsite”);
bool readLocked = site.ReadLocked;
bool readOnly = site.ReadOnly;
site.Dispose();

Ads by google