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

PnP PowerShell - Set-PnPSearchSettings - Access denied error
PnP PowerShell - Set-PnPSearchSettings - Access denied error

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))

PnP PowerShell - Set-PnPSearchSettings - Access denied error
fig : PnP PowerShell – Set-PnPSearchSettings – Access denied error

Background

  • For one of our site collection we need to hide the OOB SharePoint “Search Box” as shown in below fig.
OOB SharePoint search box
fig : OOB SharePoint Search box which we need to hide
  • 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
PowerShell – Executing CMDLET “Set-PnPSearchSettings” – “Access is denied” error – updating site collection property – DenyAddAndCustomizePages – using Set-SPOSite
fig : PowerShell – Executing CMDLET “Set-PnPSearchSettings” – “Access is denied” error – updating site collection property – DenyAddAndCustomizePages – using Set-SPOSite
  • After successfully updating property, we can successfully execute the CMDLET – Set-PnPSearchSettings
fig : PnP PowerShell – Set-PnPSearchSettings – Executed successfully

REFERENCES

Thanks for reading !

HAPPY LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading