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.