다음을 통해 공유


How to change the lock state via CSOM

PowerShell Approach

Setting the lock state for a site collection has a few rules that aren't entirely clear. You can set the lock state via PowerShell using the Set-SPOSite command:

 Set-SPOSite https://[tenant].sharepoint.com/sites/targetsite -LockState [NoAccess|Unlock]

You can read more about that on the TechNet article for it here: https://technet.microsoft.com/en-us/library/cc263238(v=office.15).aspx

Rules

The site must not be one of the following:

  • Tenant admin site (duh! obvious)
  • Root site collection (https://[tenant].sharepoint.com)
  • Search site collection (https://[tenant].sharepoint.com/search, or whatever yours is)
  • Public site (https://[tenant]-public.sharepoint.com)

CSOM Approach

To do this in CSOM, you can utilize the TenantAdministration API; however, this is only available in the v16 SharePoint Online API.

 using (var clientContext = new ClientContext(tenantUrl)) {
    clientContext.Credentials = spoCredentials;
    var tenant = new Tenant(clientContext);
    var siteProperties = tenant.GetSitePropertiesByUrl(siteUrl, true);
    clientContext.Load(siteProperties);
    clientContext.ExecuteQuery();

    Console.WriteLine("LockState: {0}", siteProperties.LockState);

    siteProperties.LockState = "Unlock";
    siteProperties.Update();
    clientContext.ExecuteQuery();
}

This will will also be a future OfficeDevPnP.Core extension (in the dev branch, now). The method is TenantExtensions.SetSiteLockState(this Tenant tenant, string siteUrl, SiteLockState lockState).

When the site is locked, you'll see it in the admin site with a lock icon next to it as seen below.

Comments

  • Anonymous
    February 20, 2017
    Hi Suman, Is there an approach to query lock state for On-Premise?
  • Anonymous
    February 20, 2017
    I believe you have to get them per site. I don't think you can use CSOM to query by properties, though that may have changed with the REST APIs.