blog.scoreman.net

Setting Site Collection to ReadOnly through CSOM

If working with SharePoint Online we have the ability to set LockState in order to set a site collection to read only. Unfortunately this is currently not available on-premises. Instead we can use Site Policies to apply a read only state to the site collection. In order to do this, create a Site Policy with the setting “Do not close or delete site automatically.” and “The site collection will be read only when it is closed.”. If you want to set read only to several site collections you can create this policy in the content type hub and have the policy published to all site collections. Once the policy exist in the site collection you can run the following CSOM code to set the site as read only:

 

clientContext.Web.ApplySitePolicy("PolicyName");
ProjectPolicy.CloseProject(clientContext, clientContext.Web);
clientContext.ExecuteQuery();

ApplySitePolicy is an extension method in OfficeDevPnP.

readonly

Leave a Reply