Microsoft 365 : PnP PowerShell – Set-PnPSearchSettings – resolving issue – Access is denied

Hi All,
Greetings for the day!!!
Today new issue and solution 🙂
Issue : “Access is denied” error while executing the PnP PowerShell CMDLET – Set-PnPSearchSettings
Set-PnPSearchSettings : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Background
- For one of our site collection we need to hide the OOB SharePoint “Search Box” as shown in below fig.

- We are using PnP PowerShell – Set-PnPSearchSettings
Set-PnPSearchSettings -SearchBoxInNavBar Hidden -Scope Site
- We have created SharePoint app using /_layouts/15/appregnew.aspx
- We have given the “Full Control” permissions using _layouts/15/appinv.aspx
<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope=" http://sharepoint/content/sitecollection"
Right="FullControl" />
</AppPermissionRequests>
- Though, we have full control to the app to our SharePoint site, we have “Access is denied” error while executing the CMDLET – Set-PnPSearchSettings
Cause of an Error
- For SharePoint site we have property called – “DenyAddAndCustomizePages“.
- This property allows us to enable or disable the custom scripting promptly for particular site collection.
- By default this property is enabled.
- By default, script is not allowed on most sites that admins create using the SharePoint admin center. It is also not allowed on all sites created using the New-SPOSite PowerShell command.
- Custom script allows users to change the look, feel, and behavior of sites and pages. This helps meet organizational objectives or individual needs.
- All users with Add and Customize Pages permission can add any script if you allow custom script. (By default, users who create sites are site owners and thus have this permission.)
- For our site this property is enabled, which is not allowing us to update the search settings.
- We can disable this property by using Set-SPOSite CMDLET.
Solution
- To make it work we can update site collection level property – DenyAddAndCustomizePages
- By default value for DenyAddAndCustomizePages property is enabled, we need to disable it
- Updating Site Collection property – using PowerShell we will use “Set-SPOSite” PowerShell CMDLET as

- After successfully updating property, we can successfully execute the CMDLET – Set-PnPSearchSettings

REFERENCES
- Microsoft 365 – Major Update – Removal of Custom Script setting in OneDrive and SharePoint web – https://knowledge-junction.in/2024/02/26/m365updatecustomscriptsetting
- Microsoft 365 – MAJOR UPDATE – SharePoint site collection admin – control for App registration / update permissions – Security measures for administrative governance – Important one – https://knowledge-junction.in/2023/10/02/microsoft-365-major-update-sharepoint-site-collection-admin-control-for-app-registration-update-permissions-security-measures-for-administrative-governance-important-one/
- Microsoft Graph – How to create / register app in Microsoft Entra – https://knowledge-junction.in/2024/05/06/msgraph-create-app-microsoft-entra/
- Microsoft Entra – registering new application and assigning permissions to access Microsoft Graph APIs – https://knowledge-junction.in/2024/01/18/microsoft-entra-registering-new-application-and-assigning-permissions-to-access-microsoft-graph-apis/
Thanks for reading !
HAPPY LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

You must be logged in to post a comment.