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

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".


SharePoint site opened in the browser

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


Prerequisites:

  1. Tenant admin access
  2. 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

do
{
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.Update()
$context.ExecuteQuery()

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

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

Disconnect-PnPOnline


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);
clientContext.Load(siteProperties);
clientContext.ExecuteQuery();

siteProperties.DenyAddAndCustomizePages = status;
var result = siteProperties.Update();
clientContext.Load(result);
clientContext.ExecuteQuery();
while (!result.IsComplete)
{
Thread.Sleep(result.PollingInterval);
clientContext.Load(result);
clientContext.ExecuteQuery();
}
}
}


Source

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