Is it possible to enable/disable external access pr 365 groups from c#. I can see that some PowerShell cmd have a property called AllowGuestsUsers but I cant find anything in Microsoft Graph or similar?
Asked
Active
Viewed 537 times
1 Answers
1
These settings are managed with the Microsoft Graph group settings. Settings can be set tenant-wide (affecting all groups), and they can be set for a specific group (affecting only that group).
Below are a couple examples using the Microsoft Graph SDK for .NET to illustrate how you can change these settings:
Disable guest users accessing Office 365 groups
The example below updates the tenant-wide settings for guest users in Office 365 group to disable adding guest users, and disable existing guest users' access to group content. It is roughly the equivalent of the steps described in Use PowerShell to control guest access.
var groupSettingsName = "Group.Unified";
// Get the group settings
var groupSettingsResult = await graph.GroupSettings.Request().GetAsync();
var groupSettings = groupSettingsResult
.FirstOrDefault(s => s.DisplayName == groupSettingsName);
// If these settings don't exist, add them (with their default values)
if (groupSettings == null)
{
// Get the settings template
var groupSettingsTemplates = await graph.GroupSettingTemplates.Request().GetAsync();
var unifiedGroupSettingTemplate = groupSettingsTemplates
.First(g => g.DisplayName == groupSettingsName);
// Create a new setting based on the template
var newGroupSettings = new GroupSetting()
{
TemplateId = unifiedGroupSettingTemplate.Id,
Values = unifiedGroupSettingTemplate.Values.Select(
v => new SettingValue()
{
Name = v.Name,
Value = v.DefaultValue
})
};
// Create the settings
groupSettings = await graph.GroupSettings.Request().AddAsync(newGroupSettings);
}
// Update settings (if needed)
var settings = groupSettings.Values.ToDictionary(x => x.Name, x => x);
if (settings["AllowToAddGuests"].Value.ToLower() != "false"
|| settings["AllowGuestsToAccessGroups"].Value.ToLower() != "false")
{
settings["AllowGuestsToAccessGroups"].Value = "false";
settings["AllowToAddGuests"].Value = "false";
await graph.GroupSettings[groupSettings.Id].Request()
.UpdateAsync(new GroupSetting() { Values = settings.Values });
}
Disable adding guest users to a specific Office 365 group
In the example below, we are setting a setting on a specific group, to disable additional guest users from being added to the group.
var groupGuestSettingsName = "Group.Unified.Guest";
// Get the group in question
var groupResult = await graph.Groups.Request()
.Filter("displayName eq 'Test_Office365_group'").GetAsync();
var group = groupResult.First();
// Get the group's settings relating to guests
var groupSettingsResult = await graph.Groups[group.Id].Settings.Request().GetAsync();
var groupSettings = groupSettingsResult
.FirstOrDefault(s => s.DisplayName == groupGuestSettingsName);
// If these settings don't exist on the group, add them (with their default values)
if (groupSettings == null)
{
// Get the settings template
var groupSettingsTemplates = await graph.GroupSettingTemplates.Request().GetAsync();
var unifiedGroupGuestSettingTemplate = groupSettingsTemplates
.First(g => g.DisplayName == groupGuestSettingsName);
// Create a new group setting based on the template
var newGroupSettings = new GroupSetting()
{
TemplateId = unifiedGroupGuestSettingTemplate.Id,
Values = unifiedGroupGuestSettingTemplate.Values.Select(
v => new SettingValue()
{
Name = v.Name,
Value = v.DefaultValue
})
};
// Add these settings to the group
groupSettings = await graph.Groups[group.Id].Settings.Request().AddAsync(newGroupSettings);
}
// Change AllowToAddGuests setting to false, if needed
var settings = groupSettings.Values.ToDictionary(x => x.Name, x => x);
if (settings["AllowToAddGuests"].Value.ToLower() != "false")
{
settings["AllowToAddGuests"].Value = "False";
await graph.GroupSettings[groupSettings.Id].Request()
.UpdateAsync(new GroupSetting() { Values = settings.Values });
}

Philippe Signoret
- 13,299
- 1
- 40
- 58
-
SWEET! I will start testing tommorow. As I read the code you add this policy to enable external access: Group.Unified.Guest. What triggers whether you disable or enable external access? – Thomas Segato May 09 '19 at 11:28
-
1Got it, its the value property. – Thomas Segato May 09 '19 at 11:31
-
Correct, the settings is a list of key-value pairs for each setting, and it so happens that in the case of `Group.Unified.Guest`, there's only one value, with key `AllowToAddGuests`. – Philippe Signoret May 09 '19 at 14:19
-
I now tried executing the code. groupSettingsResult returns 0 count and groupSettings is null. Is this because external access havent been enabled yet? – Thomas Segato May 10 '19 at 06:15
-
If [`groupSettings`](https://learn.microsoft.com/en-us/graph/api/resources/groupsetting?view=graph-rest-1.0) is empty, then all the settings will be considered to be at their default value (which you can view at [`groupSettingsTemplates`](https://learn.microsoft.com/en-us/graph/api/resources/groupsettingtemplate?view=graph-rest-1.0)) – Philippe Signoret May 10 '19 at 17:05
-
Perfect. Thanks. – Thomas Segato May 12 '19 at 13:29