Upload SitePage, got "Sorry, you don't have access to this page"

Recently I wanted to upload customized modern SitePage (.aspx) to the Office 365 SharePoint Communication site SitePages, but couldn't. Here is how to do it.

My attempt to upload the SitePage.aspx using the modern SitePages library view didn't gave me any error, rejection or feedback, but the page was not uploaded. I went to classic document library view and then got the following error: "Sorry, you don’t have access to this page".

Sorry you don't have access to this page message when try to upload sitepage.aspx to SharePoint

Now I will execute SharePointPnPPowerShellOnline PowerShell script that requires tenant admin access to make it work for the site.

- Tenant admin access
- SharePointPnPPowerShellOnline already installed and ready to execute SharePointPnPPowerShellOnline PowerShell. See how to do it on that link

Import-Module -Name SharePointPnPPowerShellOnline -DisableNameChecking

$denyAddAndCustomizePagesStatus = [Microsoft.Online.SharePoint.TenantAdministration.DenyAddAndCustomizePagesStatus]

Connect-PnPOnline -Url 'https://<tenant>-admin.sharepoint.com'

$context = Get-PnPContext

$status = $null

    Write-Host "Waiting...   $status"
    Start-Sleep -Seconds 5
    $site=Get-PnPTenantSite -url https://<tenant>.sharepoint.com/sites/<siteCollection1> -Detailed
    $status = $site.Status
} while ($status -ne 'Active')

$site.DenyAddAndCustomizePages = $denyAddAndCustomizePagesStatus::Disabled


$site=Get-PnPTenantSite -url https://<tenant>.sharepoint.com/sites/<siteCollection1> -Detailed

$state = $site.DenyAddAndCustomizePages
Write-Host "Done...site.DenyAddAndCustomizePages:  $state"


Using CSharp (C#) and OfficeDevPnP Code

    using Microsoft.Online.SharePoint.TenantAdministration;
    using Microsoft.SharePoint.Client;
    using OfficeDevPnP.Core;
        /// <summary>
        /// UpdateDenyAddAndCustomizePages
        /// </summary>
        /// <param name="tenantUrl"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="clientContext">clientContext</param>
        /// <param name="siteUrl">siteUrl</param>
        /// <param name="isEnabled">isEnabled</param>
        public static void UpdateDenyAddAndCustomizePages(string tenantUrl, string userName, SecureString password, string siteUrl, bool isEnabled = true)
            var status = DenyAddAndCustomizePagesStatus.Enabled;
            if (isEnabled == false)
                status = DenyAddAndCustomizePagesStatus.Disabled;

            var am = new AuthenticationManager();
            using (var clientContext = am.GetSharePointOnlineAuthenticatedContextTenant(tenantUrl, userName, password))
                var tenant = new Tenant(clientContext);
                var siteProperties = tenant.GetSitePropertiesByUrl(siteUrl, true);

                siteProperties.DenyAddAndCustomizePages = status;
                var result = siteProperties.Update();
                while (!result.IsComplete)

There is also community thread opened here: https://techcommunity.microsoft.com/t5/SharePoint-Developer/How-do-I-set-DenyAddAndCustomizePages-using-PnP/td-p/32372

Issues related to that execution code and Community sites: https://github.com/SharePoint/sp-dev-docs/issues/744

Sharing is Caring
Posted on

Tags: SitePage, PowerShell, Feature enable, SharePoint Online, Office 365 Dev, DenyAddAndCustomizePages, Team site, Communication site, Upload SitePage, DenyAddAndCustomizePagesStatus