locked
Why is SPSiteCollection.Restore() thrrowing Access Denied errors even when run with elevated permissions. RRS feed

  • Question

  • I am trying to do a backup and restore of a site collection via code.

    The backup creates no problem, but when I attempt a restore it get an access denied error

    Here is some code snippet:

    SPUtility.ValidateFormDigest();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        BackupSiteCollection("CustomersDefault", siteUrlToBackUp, backupPathandName, true);
        RestoreSiteCollection("CustomersDefault", siteUrlToRestore, backupPathandName, true);
    });
    
    private void BackupSiteCollection(string webAppName, string siteCollectionUrl, string backupPathandFileName, bool overwriteExisting)
    {
         SPSiteCollection siteCols = GetSiteCollectionsInWebApp(webAppName);
         siteCols.Backup(siteCollectionUrl, backupPathandFileName, overwriteExisting);
    }
    
    private void RestoreSiteCollection(string webAppName, string siteCollectionUrl, string backupPathandFileName, bool overwriteExisting)
    {
         SPSiteCollection siteCols = GetSiteCollectionsInWebApp(webAppName);
    //EXCEPTION THROWN ON THIS LINE
         siteCols.Restore(siteCollectionUrl, backupPathandFileName, overwriteExisting);
            }

    The resulting exception is

    Security Exception

    Description:
    The application attempted to perform an operation not allowed by the
    security policy.  To grant this application the required permission please
    contact your system administrator or change the application's trust level in the
    configuration file.

    Exception Details:
    System.Security.SecurityException: Access denied.

    Source
    Error:
    [No relevant source lines]

    Source
    File:
    c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET
    Files\root\a91d0bd5\cda50d7a\App_Web_changecompanyid.aspx.2afd3cbf.7tglpaaj.0.cs
       Line:
    0

    Stack Trace:
    [SecurityException: Access denied.]
       Microsoft.SharePoint.Administration.SPSiteCollection.VerifyCurrentUserIsAdministrator() +26343844
       Microsoft.SharePoint.Administration.SPSiteCollection.Restore(String strSiteUrl, String strFilename, Boolean bOverwrite, Boolean bGradualDelete, Boolean hostHeaderAsSiteName) +156
       Microsoft.SharePoint.Administration.SPSiteCollection.Restore(String strSiteUrl, String strFilename, Boolean bOverwrite) +23
       ThriveCustApplicationPages.Layouts.Customers.Administration.<>c__DisplayClass1.<btnSubmit_OnClick>b__0() +105
       Microsoft.SharePoint.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2() +729
       Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) +27947718
       Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) +27648409
       Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) +93
       ThriveCustApplicationPages.Layouts.Customers.Administration.ChangeCompanyID.btnSubmit_OnClick(Object sender, EventArgs e) +334
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11072391
       System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11071930
       System.Web.UI.Page.ProcessRequest() +91
       System.Web.UI.Page.ProcessRequest(HttpContext context) +240
       ASP._layouts_customers_administration_changecompanyid_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\a91d0bd5\cda50d7a\App_Web_changecompanyid.aspx.2afd3cbf.7tglpaaj.0.cs:0
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
    

    I've attempted to run the site with Full Trust level, but the result is the same.  Any assistance would be greatly appreciated.


    Matt Klepeis

    Monday, November 12, 2012 9:43 PM

All replies

  • Hi Matt,

    To restore a site collection, user must be site collection administrator. Please check to see if the current user (or the account under which you run Visual Studio) is not a WSS/SharePoint Farm administrator. Or this user must be a member of the following groups: AdministratorsWSS_ADMIN_WPG,WSS_RESTRICTED_WPG.

    Which version of IIS do you use? Microsoft changed the default setting of the Load User Profile setting of the application pools in Windows 7 and Windows 2008 (the old was True, in IIS7.5 it is False). Open the Advanced Settings of the Application Pool (for the web app you are using) and set the Load User Profile property to True.

    For more information, see

    http://www.primordialcode.com/blog/post/vsewss-solve-securityexception-access-denied-issue

    http://www.srinivasvangala.com/2011/03/security-exception-application.html

    Regards,

    Kelly Chen

    Tuesday, November 13, 2012 7:56 AM
  • Thanks Kelly.

    1. I am running IIS 7.5.
    2. The App Pool has Load User Profile set to true.
    3. Since running with Elevated Permissions is effectively running as a different user I also pulled the Restore out of the run with elevated permissions.  The current user attempting to run this was a Site Collection Admin, a Farm admin, and also in the 3 groups specified above. 

    The results were the same.  Still receiving an access denied error message.

    Here is another bit of information.  I am backing up a site at http://server/siteA  and attempting to restore that backup to http://server/siteB (that does not exist).  PowerShell allows you to restore a site collection to a new location, does the Restore method allow for the same?

    I also want to note that the restore fails even when I attempt to restore it to the same location.


    Matt Klepeis


    • Edited by MattKlepeis Tuesday, November 13, 2012 1:27 PM
    Tuesday, November 13, 2012 1:27 PM
  • Hi Matt,

    Did you check to see if the soure site collection is locked?

    If yes, then unlock it.

    stsadm -o setsitelock -url [url] -lock none 

    Regards,

    Kelly Chen


    Wednesday, November 14, 2012 2:35 AM