7

I created a custom action. This custom action is referencing to an application page. This application page is anonymous accessable. On this application page I have a button. When you click on this button the site collection needs to change to readonly.

I tried many solutions but I still got this access denied page. See below all solutions I tried:

Solution 1:

SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPSite site = new SPSite([Site GUID here])
        {              
            site.ReadOnly = true;
        }
    }

Solution 2:

 SPSite site = new SPSite("mysitecollectionurl.com");
                {
                    site.ReadOnly = true;
                    //Set lock comments
                    site.LockIssue = "Maintenance";
                }

Solution 3:

SPSite site = new SPSite([Site GUID here], SPUserToken.SystemAccount)
{              
    site.ReadOnly = true;
}

Solution 4:

private void LockSiteCollection(SPSite Site)
        {
            //Try lock the Portal Site Collection            
            try
            {
                Site.LockIssue = "Your Lock reason";
            }
            //handle prior left out read only locks due to errors.                
            catch (UnauthorizedAccessException)
            {
                Site.ReadOnly = false;
                Site.LockIssue = "Your Lock reason";
            }
            Site.ReadOnly = true;
        }
  • i am not developer but are you trying to mark a site collection as readonly within that site or from outside? – Waqas Sarwar MVP Jul 1 '15 at 12:34
  • hi, within that site. – Ola Jul 1 '15 at 12:35
  • 1
    i dont think it is possible, you trying to mark that site as read-only which you are browsing...i think you can try to run that code with farm admin account.. – Waqas Sarwar MVP Jul 1 '15 at 12:37
  • I try it with the farm account like the script below. Same access denied page. – Ola Jul 2 '15 at 11:16
1

@Waqas is correct - this is a privileged actions. When you use the RunWithElevatedPrivileges at the site level it will execute as the application pool account not the farm account. If you run SQL Trace you would see it making the request as this account.

You could try the following - specifying the farm account as your user:

SPUser user = SPContext.Current.Web.SiteUsers[@"domain\username"];
using(SPSite oSiteCollection = new SPSite("http://siteUrl/", user.UserToken)){
     //Try lock the Portal Site Collection            
        try
        {
            Site.LockIssue = "Your Lock reason";
        }
        //handle prior left out read only locks due to errors.                
        catch (UnauthorizedAccessException)
        {
            Site.ReadOnly = false;
            Site.LockIssue = "Your Lock reason";
        }
        Site.ReadOnly = true;
}

One approach I've used in the past is to capture the request in a list, and then schedule PowerShell to parse that list at specific intervals and execute the commands I need.

  • I try your code and used the farm account. Also now I got the access denied page. Is there someone who can test this code on he's machine? I do not know 100 % if it should work or not. – Ola Jul 2 '15 at 11:16
0

Actually It's normal, you can't do that (sharepoint disallow that).

You can't do that from the SPSite. This property is only editable from the Central Admin (or in powershell). (It's a Administration property, so if you update this value from the site collection, SharePoint automatically throw an Acces Denied even if you are Site collection Administrator, or in Elevated Context ...).

If you have a look at the implementation of the property SPSite.ReadOnly with justDecompile (for exemple), it call : the internal methode SaveFlag with the parameter "requireCentralAdmin" to true.enter image description here

Hope this help you to understand why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.