While working with SharePoint 2010 or SharePoint 2007, there are some scenarios where you want to take your content database of your web application to a read only state.

For example when?

That's a normal question one can ask, and the answer is, when you / IT administrators are doing some migrations of your content database to some other system (like we do in SharePoint 2007 upgrades to SharePoint 2010 by using the database attach upgrade process), or you / IT administrators are doing some maintenance of your SQL server.

Practically doing this, you are making sure that end users are not adding contents to your site while you are working / migrating with content database and the good thing about SharePoint is that when you set your content database to read only state then SharePoint recognizes that and removes / hides all controls which cause any "Add" action to the site.

Well, you can do this in three ways:

a. by using stsadm

b. by using visual studio

c. by using SQL Server Management Studio (easy for IT admins)

How to do this using stsadm?

Here is a simple command which will put your site collection to read only mode and users won't be able to add new content.

For read only site:

stsadm.exe "o setsitelock" url http://yoursite/ -lock readonly

for bringing site back to normal:

stsadm.exe "o setsitelock" url http://yoursite/ -lock none

Details on Technet

How to do this using Visual Studio? (I love this)

static void Main(string[] args)

{

try

{

using (SPSite site = new SPSite("http://yoursite/"))

{

site.AllowUnsafeUpdates = true;

site.ReadOnly = true;

Console.WriteLine(site.ReadOnly.ToString());

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

Console.ReadLine();

}

How can I do this as IT administrator?

Ok this is good so far but what's in it for me as developer??

Yes interesting question, and obvious answer; while working with SharePoint and customizing / developing on top of SharePoint platform we usually create some web controls, web parts and so on, but one thing we should always consider in the aspect of a read only databases is, we should disable any "Add" functionality to site when database is in read only state.

For example, I have a web part which adds some data to SharePoint list when clicked, and suppose the site database is in a read only state for some reason, what will happen? Error, correct?

So to avoid this, we should always check the database status of the site like this:

if (!site.WebApplication.ContentDatabases[0].IsReadOnly)

{

if (!site.ReadOnly)

{

//Enable Add/Edit functionality

}

}

else

{

//Disable Add/Edit functionality

}