SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

SPSite ReadOnly & WriteLocked properties fail on a GET request

So the following lines of code look pretty simple:

SPSite site = new SPSite(sSiteURL);
...
web.Site.ReadOnly = true;

...

What's even better is that they actually work.  Until that is they become part of a GET request (i.e. from a web page).  If your application is web based and this code is run as the result of a get request then the operation will fail with an error such as:

Updates are currently disallowed on GET requests.  To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.

I tried just about everything I know to get it going but it wouldn't work.  Some examples of the code I tried follow:

Attempt 1:

SPSiteAdministration siteAdm = new SPSiteAdministration(sSiteURL);
siteAdm.LockIssue = "test";
siteAdm.WriteLocked = true;
siteAdm.Dispose();

Attempt 2:

using (Identity.ImpersonateAdmin())
            {
                SPGlobalAdmin ga = new SPGlobalAdmin();
                SPVirtualServer v = ga.OpenVirtualServer(new Uri(sTopSite));

                for (int x = 0; x < v.Sites.Count; x++)
                {
                    if (v.Sites[x].Url.ToUpper() == sSiteURL.ToUpper())
                    {
                        v.Sites[x].AllowUnsafeUpdates = true;
                        v.Sites[x].LockIssue = "Locked through code";
                        v.Sites[x].WriteLocked = true;
                        v.Sites[x].Close();
                        break;
                    }
                }
            }

Attempt 3:

SPSite site = new SPSite(sSiteURL);
site.AllowUnsafeUpdates = true;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
web.Update();

web.Site.WriteLocked = true;

web.Update();
web.Close();

Attempt 4:

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPSite site = new SPSite(sSiteURL);
                SPWeb web = site.OpenWeb();
                web.AllowUnsafeUpdates = true;
                site.AllowUnsafeUpdates = true;
                web.Update();

                web.Site.ReadOnly = true;

                web.Update();
                web.Close();
            });
            return true;

Attempt 5:

        public bool lockSite(string sSiteURL)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite _SPSite = new SPSite(sSiteURL))
                {
                    _SPSite.WriteLocked = true;
                }
            });
            return true;
        }

In the end I put it down as a bug and rewrote the web application to make a post request (via a button) and hey presto, it works fine.


Posted 01-21-2008 4:20 PM by Andrew Noon

Comments

SharePoint Link Love: 01-27-2008 part six at Virtual Generations wrote SharePoint Link Love: 01-27-2008 part six at Virtual Generations
on 01-27-2008 8:13 AM

Pingback from  SharePoint Link Love: 01-27-2008 part six at  Virtual Generations

SharePoint Link Love: 01-27-2008 part four at Virtual Generations wrote SharePoint Link Love: 01-27-2008 part four at Virtual Generations
on 01-27-2008 8:21 AM

Pingback from  SharePoint Link Love: 01-27-2008 part four at  Virtual Generations

Garth wrote re: SPSite ReadOnly & WriteLocked properties fail on a GET request
on 03-20-2008 7:41 AM
Pesi wrote re: SPSite ReadOnly & WriteLocked properties fail on a GET request
on 04-11-2008 1:18 AM

Hi Andrew,

I am trying to do something similar. I want to disable editing across the whole site collection after a particular step in the Workflow is completed i.e. after the verification is completed. Dont know how? Any help is appreciated.

Thank you!!

Vince wrote re: SPSite ReadOnly & WriteLocked properties fail on a GET request
on 04-15-2008 11:14 PM

Hi Andrew,

I ran the following code and my application crashed. It does not allow me to login even using the Admin account.

SPSecurity.RunWithElevatedPrivileges(delegate()

           {

               SPSite site = new SPSite(sSiteURL);

               SPWeb web = site.OpenWeb();

               web.AllowUnsafeUpdates = true;

               site.AllowUnsafeUpdates = true;

               web.Update();

               web.Site.ReadOnly = true;

               web.Update();

               web.Close();

           });

Can you please tell me how to get it back up.

Thanks.

Andrew Noon wrote re: SPSite ReadOnly & WriteLocked properties fail on a GET request
on 04-20-2008 1:32 AM

I assume you've already run the code with:

web.Site.ReadOnly = false;

????????????

spsite wrote spsite
on 05-09-2008 5:17 PM

Pingback from  spsite

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.