I get asked a few times on how to address a scenario where want to enable external sharing for team collaboration sites, however we do not want to enable external sharing for our OneDrive for Business Sites.


First off, External sharing for OneDrive for Business is an all or nothing approach and is enabled by default. To disable External Sharing you would navigate to your SharePoint Admin Tenant URL, Site Collection view and choose Sharing from the ribbon. You will be presented with a similar view as depicted below where you can adjust your settings.

Once you disable sharing users will no longer be able to share outside your organization. Now you can disable Sharing at the tenant level but that will work but we still have that scenario that we want to allow Sharing for our collaboration sites.

Now, I'm a developer at heart and I cant have a blog post without sharing some code. So lets say for your team collaboration sites, you have a custom site provisioning solution like the one at https://officeams.codeplex.com/ and you wanted to enable External Sharing in these sites during the provisioning process and you don't want to have to navigate and enable Sharing for each one of your sites You can easy update the solution to include the below code. Now, don't try this one a OneDrive site you will receive an error.

/// <summary>
/// Sets the Site Collection External Sharing Setting using the SharePoint Tenant API
/// </summary>
/// <param name="adminCC"></param>
/// <param name="siteCollectionURl"></param>
/// <param name="shareSettings"></param>
public static void SetSiteSharing(ClientContext adminCC, string siteCollectionURl, SharingCapabilities shareSettings)
{
var _tenantAdmin = new Tenant(adminCC);
SiteProperties _siteprops = _tenantAdmin.GetSitePropertiesByUrl(siteCollectionURl, true);
adminCC.Load(_tenantAdmin);
adminCC.Load(_siteprops);
adminCC.ExecuteQuery();

SharingCapabilities _tenantSharing = _tenantAdmin.SharingCapability;
var _currentShareSettings = _siteprops.SharingCapability;
bool _isUpdatable = false;

if(_tenantSharing == SharingCapabilities.Disabled)
{
Console.WriteLine("Sharing is currently disabled in your tenant! I am unable to work on it.");
}
else
{
if(shareSettings == SharingCapabilities.Disabled)
{ _isUpdatable = true; }
else if(shareSettings == SharingCapabilities.ExternalUserSharingOnly)
{
_isUpdatable = true;
}
else if (shareSettings == SharingCapabilities.ExternalUserAndGuestSharing)
{
if (_tenantSharing == SharingCapabilities.ExternalUserAndGuestSharing)
{
_isUpdatable = true;
}
else
{
Console.WriteLine("ExternalUserAndGuestSharing is currently disabled in your tenant! I am unable to work on it.");
}
}
}
if (_currentShareSettings != shareSettings && _isUpdatable)
{
_siteprops.SharingCapability = shareSettings;
_siteprops.Update();
adminCC.ExecuteQuery();
Console.WriteLine("Set Sharing on site {0} to {1}.", siteCollectionURl, shareSettings);
}
}

You can get the sample solution here.

Sharing is caring!