How to fix the ACCESS DENIED error when restoring backed-up SharePoint data

Problem:

When restoring backed-up SharePoint sites by using CodeTwo Backup, you might receive the following or similar error:

Access denied. You do not have permission to perform this action or access this resource.

The error prevents the program from finishing the restore job successfully.

Solution:

This error is connected with the Custom Script settings in the SharePoint admin center. Running custom script allows users to customize SharePoint sites. By default, custom script is allowed only on sites created by SharePoint admins but blocked on sites created by users, on OneDrive sites, and on the root site collection. If custom script is blocked, it prevents CodeTwo Backup from restoring the backed-up content.

To fix this error, you need to allow custom script on a particular site to which you are restoring your backed-up data. For OneDrive sites (personal sites), you only need to enable custom script globally in the SharePoint admin center. For other sites (team sites and site collections), you need to change the Custom Script settings in the SharePoint admin center as well as enable custom script for individual sites by using SharePoint Online Management Shell.

Follow the links below to learn how to:

Warning

Before you enable custom script, you need to be aware of the security implications of doing so. See this Microsoft article to learn more.

Allowing custom script in the SharePoint admin center

  1. Log in to Office 365 and click Admin to open Microsoft 365 admin center (Office 365 admin center).
  2. In the left pane, expand Admin centers and click SharePoint (Fig. 1.).

    740-1
    Fig. 1. Opening the SharePoint admin center.

  3. In the SharePoint admin center, cllick Settings in the menu on the left, and then click the classic settings page link at the bottom of the page (Fig. 2.).

    740-2
    Fig. 2. Accessing the classic SharePoint admin center settings.

  4. Scroll down to the Custom Script settings (Fig. 3.) and select the following options:
    • Allow users to run custom script on personal sites
    • Allow users to run custom script on self-service created sites

    740-3
    Fig. 3. The Custom Script settings.

  5. Click OK to save changes.

Important

You may need to wait up to 24 h for the changes to custom script made in the SharePoint admin center to propagate.

Allowing custom script by using SharePoint Online Management Shell

  1. Open the ps SharePoint Online Management Shell command prompt.
  2. Connect to your SharePoint Online by using the following command:
    Connect-SPOService -Url https://<organization>-admin.sharepoint.com -Credential <admincredential>
    where <organization> is the organization name set in Office 365, and <admincredential> is the email address of your Office 365 global administrator or SharePoint administrator. Enter the admin password when prompted.
  3. Run the following cmdlet:
    Set-SPOsite <siteURL> -DenyAddAndCustomizePages 0
    where <siteURL> is the URL of the specific SharePoint site collection or team site.

Important

Changes to the Custom Script settings made by using SharePoint Online Management Shell take effect immediately. However, these changes will be overwritten by the Custom Script settings in the SharePoint admin center within 24 h, so make sure that you have completed these steps as well.