Friday, June 10, 2016

Site accessible only for Site Collection Administrator

One of the simple questions (seems to be simple at first) has been asked in SharePoint MSDN forum, "How to ensure that a site collection only can be accessible by "Site Collection Administrator".

While doing the brain storming analyse on this topic, my first stop was finding the solution through SharePoint Power Shell.

But lastly I found there is no such cmdlets for this scenario.

Here are the possible workaround for this,

Creating the feature event receiver that will check the currently logged in user belongs to Site collection administrator , if not set the "ReadLock" on the site collection.

This will help the site collection administrator for quick maintenance to be done on the site on peak hours.

Upon activating the feature, Site will be ready to check the currently logged in user is belongs to Site collection administrator or an end user.

On Deactivating lock will be removed for end user.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            SPWeb web = site.OpenWeb();
            if (!web.CurrentUser.IsSiteAdmin)
            {
                site.ReadLocked = false;
             
            }
            else
            {
                site.ReadLocked = true;
            }
         
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
           //Remove the lock for all
         
        }

Approach : 2