SOLVED

Issue Setting Domains AllowList for Sharing in CSOM API

%3CLINGO-SUB%20id%3D%22lingo-sub-366711%22%20slang%3D%22en-US%22%3EIssue%20Setting%20Domains%20AllowList%20for%20Sharing%20in%20CSOM%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-366711%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20found%20an%20issue%20trying%20to%20set%20allowed%20domains%20for%20sharing%20through%20CSOM.%20When%20setting%20the%20site%20property's%26nbsp%3B%3CSPAN%3ESharingDomainRestrictionMode%3C%2FSPAN%3E%20to%20AllowList%20and%20setting%20the%26nbsp%3B%3CSPAN%3ESharingAllowedDomainList%3C%2FSPAN%3E%20all%20queries%20are%20executed%20without%20error%2C%20however%20when%20looking%20at%20the%20sharing%20settings%20in%20SharePoint%20the%3C%2FP%3E%3CP%3E%22%3CSPAN%3ELimit%20external%20sharing%20by%20domain.%3C%2FSPAN%3E%22%3C%2FP%3E%3CP%3Echeckbox%20is%20checked%2C%20but%20the%20mode%20is%20set%20to%3C%2FP%3E%3CP%3E%22%3CSPAN%3EDon't%20allow%20sharing%20with%20users%20from%20these%20blocked%20domains%3C%2FSPAN%3E%22%3C%2FP%3E%3CP%3Eand%20no%20domains%20are%20in%20the%20list.%20The%20BlockList%20%3CSPAN%3ESharingDomainRestrictionMode%3C%2FSPAN%3E%20works%2C%20and%20the%20SharingBlockedDomainList%20can%20be%20set.%26nbsp%3B%3CSPAN%3ESharingDomainRestrictionMode%3C%2FSPAN%3E%20None%20also%20works%2C%20only%20the%20AllowList%20does%20not%20work%20as%20intended.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EDoes%20anyone%20have%20any%20idea%20what's%20going%20on%20here%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20498px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F87570iD9603EF5188959CC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22sharing.PNG%22%20title%3D%22sharing.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Evar%20siteProperties%20%3D%20tenant.GetSitePropertiesByUrl(siteCollectionUrl%2C%20false)%3B%3CBR%20%2F%3Ectx.Load(siteProperties%2C%20s%20%3D%26gt%3B%20s.SharingDomainRestrictionMode%2C%20s%20%3D%26gt%3B%20s.SharingAllowedDomainList)%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3CBR%20%2F%3E%3CBR%20%2F%3EsiteProperties.SharingDomainRestrictionMode%20%3D%20%3CSPAN%3ESharingDomainRestrictionModes.AllowList%3C%2FSPAN%3E%3B%3CBR%20%2F%3EsiteProperties.SharingAllowedDomainList%20%3D%20domainString%3B%3CBR%20%2F%3E%3CBR%20%2F%3EsiteProperties.Update()%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-366711%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPIs%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ECSOM%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-485614%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%20Setting%20Domains%20AllowList%20for%20Sharing%20in%20CSOM%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-485614%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F326131%22%20target%3D%22_blank%22%3E%40Borup-DK%3C%2FA%3E%3C%2FP%3E%3CP%3EThanks%20for%20the%20reply.%3C%2FP%3E%3CP%3EIs%20it%20in%20Classic%20or%20Modern%2C%20where%20it%20works%20for%20you%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-481766%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%20Setting%20Domains%20AllowList%20for%20Sharing%20in%20CSOM%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-481766%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F299978%22%20target%3D%22_blank%22%3E%40davidrosmon%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20below%20works%20for%20me%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETenant%20tenant%20%3D%20new%20Tenant(context)%3B%3CBR%20%2F%3Evar%20siteProperties%20%3D%20tenant.GetSitePropertiesByUrl(url%2C%20true)%3B%3CBR%20%2F%3Econtext.Load(siteProperties)%3B%3CBR%20%2F%3Econtext.ExecuteQuery()%3B%3CBR%20%2F%3EsiteProperties.SharingDomainRestrictionMode%20%3D%20Microsoft.Online.SharePoint.TenantManagement.SharingDomainRestrictionModes.AllowList%3B%3CBR%20%2F%3EsiteProperties.SharingAllowedDomainList%20%3D%20%22google.com%22%3B%3CBR%20%2F%3EsiteProperties.Update()%3B%3CBR%20%2F%3Econtext.ExecuteQuery()%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-497535%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%20Setting%20Domains%20AllowList%20for%20Sharing%20in%20CSOM%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-497535%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F299978%22%20target%3D%22_blank%22%3E%40davidrosmon%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20on%20a%20Modern%20Site%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-552394%22%20slang%3D%22en-US%22%3ERe%3A%20Issue%20Setting%20Domains%20AllowList%20for%20Sharing%20in%20CSOM%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-552394%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20you%20want%20to%20switch%20between%20block%20and%20allow%20mode%2C%20you%20need%20to%20set%20the%20mode%20to%20None%2C%20then%20execute%20and%20the%20set%20it%20to%20whatever%20your%20looking%20to%20do.%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20siteProperties%20%3D%20tenant.GetSitePropertiesByUrl(siteCollectionUrl%2C%20false)%3B%3CBR%20%2F%3Ectx.Load(siteProperties%2C%20s%20%3D%26gt%3B%20s.SharingDomainRestrictionMode%2C%20s%20%3D%26gt%3B%20s.SharingAllowedDomainList)%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3CBR%20%2F%3E%3CBR%20%2F%3EsiteProperties.SharingDomainRestrictionMode%20%3D%20SharingDomainRestrictionModes.None%3B%3CBR%20%2F%3EsiteProperties.Update()%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3C%2FP%3E%3CP%3EsiteProperties.SharingDomainRestrictionMode%20%3D%20SharingDomainRestrictionModes.AllowList%3B%3CBR%20%2F%3EsiteProperties.SharingAllowedDomainList%20%3D%20domainString%3B%3CBR%20%2F%3EsiteProperties.Update()%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi all

 

We found an issue trying to set allowed domains for sharing through CSOM. When setting the site property's SharingDomainRestrictionMode to AllowList and setting the SharingAllowedDomainList all queries are executed without error, however when looking at the sharing settings in SharePoint the

"Limit external sharing by domain."

checkbox is checked, but the mode is set to

"Don't allow sharing with users from these blocked domains"

and no domains are in the list. The BlockList SharingDomainRestrictionMode works, and the SharingBlockedDomainList can be set. SharingDomainRestrictionMode None also works, only the AllowList does not work as intended.

Does anyone have any idea what's going on here?

 

sharing.PNG

 

var siteProperties = tenant.GetSitePropertiesByUrl(siteCollectionUrl, false);
ctx.Load(siteProperties, s => s.SharingDomainRestrictionMode, s => s.SharingAllowedDomainList);
ctx.ExecuteQueryRetry();

siteProperties.SharingDomainRestrictionMode = SharingDomainRestrictionModes.AllowList;
siteProperties.SharingAllowedDomainList = domainString;

siteProperties.Update();
ctx.ExecuteQueryRetry();

 

4 Replies

@davidrosmon 

 

The code below works for me:

 

Tenant tenant = new Tenant(context);
var siteProperties = tenant.GetSitePropertiesByUrl(url, true);
context.Load(siteProperties);
context.ExecuteQuery();
siteProperties.SharingDomainRestrictionMode = Microsoft.Online.SharePoint.TenantManagement.SharingDomainRestrictionModes.AllowList;
siteProperties.SharingAllowedDomainList = "google.com";
siteProperties.Update();
context.ExecuteQuery();

@Borup-DK

Thanks for the reply.

Is it in Classic or Modern, where it works for you?

@davidrosmon 

 

It is on a Modern Site

best response confirmed by davidrosmon (New Contributor)
Solution

If you want to switch between block and allow mode, you need to set the mode to None, then execute and the set it to whatever your looking to do.

var siteProperties = tenant.GetSitePropertiesByUrl(siteCollectionUrl, false);
ctx.Load(siteProperties, s => s.SharingDomainRestrictionMode, s => s.SharingAllowedDomainList);
ctx.ExecuteQueryRetry();

siteProperties.SharingDomainRestrictionMode = SharingDomainRestrictionModes.None;
siteProperties.Update();
ctx.ExecuteQueryRetry();

siteProperties.SharingDomainRestrictionMode = SharingDomainRestrictionModes.AllowList;
siteProperties.SharingAllowedDomainList = domainString;
siteProperties.Update();
ctx.ExecuteQueryRetry();