To lock or unlock a SharePoint Online site

30 Oct

SharePoint sites can be locked or put in read-only mode.
In this post PowerShell is used to lock a site. Another option is to use the Site Policies settings of the site.

The PowerShell commands are quite straightforward.
First connect to a site:

Connect-PnPOnline $siteUrl

Lock the site by using:

Set-PnPSite -LockState NoAccess

When the site is accessed once locked a 403 forbidden response is displayed in the browser as shown in the following figure.

403 forbidden

You can make it more fancy to redirect the visitor of the site to another url by setting this tenant-wide using the command:

Set-PnPTenant -NoAccessRedirectUrl "<your url>"

Now the visitor doesn’t see the 403 response, but another webpage as configured.

At a certain point in time once mind has changed and the has to be unlocked.

Well, the command again is straightforward after reconnecting to the site:

Set-PnPSite -LockState Unlock

But the response is:

Set-PnPSite : The remote server returned an error: (403) Forbidden.
At line:1 char:1
+ Set-PnPSite -LockState Unlock -Connection $siteConnection
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Set-PnPSite], WebException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Site.SetSite

Which actually makes sense, but

How to unlock the site then?

In PowerShell the connection is made with the account which has assigned the Global admininistrator role.

In the SharePoint admin center, logged in with that same account, the locked site is listed and marked as locked in the properties panel as shown below.

Admin center shows a site is locked

These actions were performed on a Teamsite with a group, a Teamsite without a group and a Communication site, all with the same result: they are all inaccessible in any way now.

Please help me out and put your suggestions in the comments below and I’ll get back to it!

Leave a Reply

Your email address will not be published. Required fields are marked *

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