Forum Discussion
Christopher Johnson
Jun 14, 2017Brass Contributor
Access denied in SharePoint SitePages library
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.
- Jonathan CardyCopper Contributor
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-customize-pages
- GlennE888Copper Contributor
Does that work for you? I tried to add the page via Files.AddTemplateFile as mentioned but I get the same Access Denied Error.
- Jonathan CardyCopper Contributor
GlennE888 yes it worked for me!
- Richard BourkeIron Contributor
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.
- Christopher JohnsonBrass ContributorThanks 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.
- Marc VanderstraetenBrass Contributor