Access denied in SharePoint SitePages library

Brass Contributor

I am trying to create modern SharePoint pages programmatically in SitePages libraries on groups-based sites.

 

I first attempted doing so with PowerShell, then with Flow and finally confirmed via a direct call to the REST service that it is not supported-- in each case, the result is the same: Access Denied.

 

Is there a policy on modern SharePoint SitePages libraries that prevents users-- even users with full control of the site-- from creating content via REST?

 

Below is the result of the REST post to /files/add method:

{ "status": 403, "message": "Access denied. You do not have permission to perform this action or access this resource.", "source": "https://<myTenantUrl>.sharepoint.com/sites/<siteName>/_api/web/GetFolderByServerRelativeUrl('/sites/Safety/SitePages')/files/add(url='Test9.aspx',%20overwrite='true')", "errors": [ "-2147024891", "System.UnauthorizedAccessException" ] }

"-2147024891", "System.UnauthorizedAccessException" ] }

 

This appears related to the same issue that I posted to the PnP GitHub repo, in which the same occurs when attempting to upload a page via PowerShell.

 

Per this post by @Vesa Juvonen, the client object model supports adding pages programmatically and there are PnP extension methods to do so. 

 

TL;DR: Just want to confirm that the same is NOT possible via REST at this time as it will affect the architecture of a solution I'm writing.

 
10 Replies

Just tried this on my tenant, I can upload via REST to Site Pages on classic sites, I get the same behaviour as you describe when trying on a Modern site.

Thanks for double-checking me. One comment on my PnP query suggested that this is intentional to prevent developers from uploading ASPX pages. Not sure if this is the case or not; hoping we can get some kind of confirmation from Microsoft.

I had the same issue trying with CSOM... Any solution?

What I saw in the permissions is that there is a DENY ... Another indicator that something is blocking us...


DenyDeny

Trying to upload an aspx file directly will raise the Access Denied error, but you can use the `Files.AddTemplateFile` method do it: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/modern-experience-customizations-c...

@Jonathan Cardy 

 

Does that work for you?  I tried to add the page via Files.AddTemplateFile as mentioned but I get the same Access Denied Error.

@GlennE888 yes it worked for me!

@Jonathan Cardy Thanks for the response.  Is your Sharepoint OnPremise or Office365?  I'm trying to upload to Office365 and with the example given I continue to get "Access denied. You do not have permission to perform this action or access this resource."

 

I'm giving OfficeDevPnP.Core.Pages a try and am having a bit more luck with that.

I'm using Office365. I haven't tried it on premise as of yet but I believe the solution is the same. PnP does actually use this same method - compare your code to that. Source is here - search for 'AddTemplateFile': https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Pages/ClientSidePage...

@Jonathan Cardy

Thanks, I got it working, I was pointing to the folder where I wanted to place the file instead of pointing to the file I wanted to be created. So I now am able to create pages on the site but still having problems with the pages created.


If I create the page in Site Pages, then try to edit them from the UI I get:

"You cannot edit this page

We're sorry, we encountered an unexpected error. Please try again, if the problem persists please contact your Administrator."

 

If I create the page in WikiPageLib, I can't even load the file. Attempting to load the file I get:

"Sorry, something went wrong
NoComponentId"


Any ideas?


Thanks.

@GlennE888 Sounds like the source of the page might be messed up? It might be worth opening a new ticket.