SharePoint Read Only Databases and me as Developer

n

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

n

nFor example when?

n

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

n

nPractically doing this, you are making sure that end users are not adding contents to your site while you are working / migrating with content database

n

nand good thing about SharePoint is , 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 site

n

nWell, you can do this in three ways :

n

na.    by using stsadm

n

nb.    by using visual studio

n

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

n

nHow to do this using stsadm?

n

nHere is simple command which will put your site collection to read only mode and users wont be able to add new content

n

nFor read only site:

n

nstsadm.exe –o setsitelock –url http://yoursite/ -lock readonly

n

nfor bringing site back to normal :

n

nstsadm.exe –o setsitelock –url http://yoursite/ -lock none

n

n

n

n

n

n

nHow to do this using Visual Studio? (I love this J )     

n

n

n

nstatic void Main(string[] args)

n

n{

n

n try

n

n {

n

n   using (SPSite site = new SPSite(“http://yoursite/”))

n

n   {

n

n     site.AllowUnsafeUpdates = true;

n

n     site.ReadOnly = true;

n

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

n

n   }

n

n }

n

n catch (Exception ex)

n

n {

n

n     Console.WriteLine(ex.Message);

n

n }

n

n

n

n   Console.ReadLine();

n

n }

n

n

n

n

n

nHow can I do this as IT administrator?

n

n

n

n·         Well that’s a pretty simple task if you having SQL Server Management Studio installed. Here are steps to do this

n

n·         Open SQL Server Management Studio (for example for SQL Server 2008) and connect to the instance of database which is having your SharePoint content databases

n

n·         Once you find your web application’s content database, right click on it and then select properties, now a small pop up window should open

n

n·         in this new window , select Options from left menus and find option Database Read only and set that to true and you are done

n

n

n

nOk this is good so far but what’s in it for me as developer??

n

nYes interesting question, and obvious answer

n

nwhile 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 aspect of read only databases is , we should disable any “Add” functionality to site when database is in read only state

n

nFor example, I have a web part which adds some data to SharePoint list when clicked , and suppose site database is in read only state for some instance ,what will happen? Error isn’t it?

n

nSo to avoid this, we should always check database status of site like this

n

n

n

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

n

n{

n

n  if (!site.ReadOnly)

n

n  {

n

n    //Enable Add/Edit functionality

n

n  }

n

n}

n

nelse

n

n{

n

n    //Disable Add/Edit functionality

n

n}

n

Leave a Comment