Beispiel #1
0
        /// <summary>
        /// Retrieves the current value for the Site Classification of a Site Collection
        /// </summary>
        /// <param name="site">The target site</param>
        /// <param name="classificationValue">The new value for the Site Classification</param>
        /// <param name="accessToken">The OAuth Access Token to consume Microsoft Graph, required only for GROUP#0 site collections</param>
        /// <param name="azureEnvironment">Defines the Azure Cloud Deployment. This is used to determine the MS Graph EndPoint to call which differs per Azure Cloud deployments. Defaults to Production (graph.microsoft.com).</param>
        /// <returns>The classification for the site</returns>
        public static void SetSiteClassification(this Site site, string classificationValue, string accessToken = null, AzureEnvironment azureEnvironment = AzureEnvironment.Production)
        {
            // Determine the modern site template
            var baseTemplateValue = site.RootWeb.GetBaseTemplateId();

            switch (baseTemplateValue)
            {
            // It is a "modern" team site
            case "GROUP#0":

                if (String.IsNullOrEmpty(accessToken))
                {
                    throw new ArgumentNullException(nameof(accessToken));
                }

                // Ensure the GroupId value
                site.EnsureProperty(s => s.GroupId);

                // Update the Classification of the Office 365 Group
                // PATCH https://graph.microsoft.com/beta/groups/{groupId}
                string updateGroupUrl    = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment,true)}groups/{site.GroupId}";
                var    updateGroupResult = GraphHttpClient.MakePatchRequestForString(
                    updateGroupUrl,
                    content: new
                {
                    classification = classificationValue
                },
                    contentType: "application/json",
                    accessToken: accessToken);

                // Still update the local value to give prompt feedback to the user
                site.Classification = classificationValue;
                site.Context.ExecuteQueryRetry();

                break;

            // It is a "modern" communication site
            case "SITEPAGEPUBLISHING#0":
            default:

                site.Classification = classificationValue;
                site.Context.ExecuteQueryRetry();

                break;
            }
        }
Beispiel #2
0
        public static TokenParser ProcessO365GroupSettings(Tenant tenant, ProvisioningTenant provisioningTenant, TokenParser parser, PnPMonitoredScope scope, ProvisioningMessagesDelegate messagesDelegate)
        {
            if (provisioningTenant.Office365GroupsSettings != null && provisioningTenant.Office365GroupsSettings.Properties.Any())
            {
                messagesDelegate?.Invoke("Processing Office 365 Group Settings", ProvisioningMessageType.Progress);
                bool siteClassificationSettingsExists = false;
                if (PnPProvisioningContext.Current != null)
                {
                    string accessToken = string.Empty;
                    try
                    {
                        // Get a fresh Access Token for every request
                        accessToken = PnPProvisioningContext.Current.AcquireToken(GraphHelper.MicrosoftGraphBaseURI, "Directory.ReadWrite.All");

                        if (accessToken != null)
                        {
                            try
                            {
                                var siteClassificationSettings = tenant.GetSiteClassificationsSettings(accessToken);
                                siteClassificationSettingsExists = true;
                            }
                            catch (Exception)
                            {
                                // Tenant classification doesn't exist, just swallow the exception.
                            }

                            var azureEnvironment = AzureEnvironment.Production;
                            if (PnPProvisioningContext.Current != null)
                            {
                                azureEnvironment = PnPProvisioningContext.Current.AzureEnvironment;
                            }

                            if (siteClassificationSettingsExists)
                            {
                                // Tenant classification exists, update the necessary values for Group Settings.
                                try
                                {
                                    string directorySettingTemplatesUrl  = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment)}groupSettings";
                                    var    directorySettingTemplatesJson = GraphHttpClient.MakeGetRequestForString(directorySettingTemplatesUrl, accessToken);
                                    var    directorySettingTemplates     = JsonConvert.DeserializeObject <DirectorySettingTemplates>(directorySettingTemplatesJson);

                                    // Retrieve the setinngs for "Group.Unified"
                                    var unifiedGroupSetting = directorySettingTemplates.Templates.FirstOrDefault(t => t.DisplayName == "Group.Unified");

                                    if (unifiedGroupSetting != null)
                                    {
                                        var props = provisioningTenant.Office365GroupsSettings.Properties;
                                        foreach (var v in unifiedGroupSetting.SettingValues)
                                        {
                                            var item = props.Where(p => p.Key == v.Name).FirstOrDefault();
                                            if (!string.IsNullOrEmpty(item.Key))
                                            {
                                                v.Value = parser.ParseString(item.Value);
                                            }
                                        }

                                        string updateDirectorySettingUrl    = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment)}groupSettings/{unifiedGroupSetting.Id}";
                                        var    updateDirectorySettingResult = GraphHttpClient.MakePatchRequestForString(
                                            updateDirectorySettingUrl,
                                            content: new
                                        {
                                            templateId = unifiedGroupSetting.Id,
                                            values     = from v in unifiedGroupSetting.SettingValues select new { name = v.Name, value = v.Value },
                                        },
                                            contentType: "application/json",
                                            accessToken: accessToken);
                                    }
                                    else
                                    {
                                        throw new ApplicationException("Missing DirectorySettingTemplate for \"Group.Unified\"");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    scope.LogError($"Error occurred processing O365 Group settings ${ex.Message}");
                                }
                            }
                            else
                            {
                                // Tenant classification doesn't exist, create the necessary template for Group Settings.

                                try
                                {
                                    string directorySettingTemplatesUrl  = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment)}groupSettingTemplates";
                                    var    directorySettingTemplatesJson = GraphHttpClient.MakeGetRequestForString(directorySettingTemplatesUrl, accessToken);
                                    var    directorySettingTemplates     = JsonConvert.DeserializeObject <DirectorySettingTemplates>(directorySettingTemplatesJson);

                                    // Retrieve the setinngs for "Group.Unified"
                                    var unifiedGroupSetting = directorySettingTemplates.Templates.FirstOrDefault(t => t.DisplayName == "Group.Unified");

                                    if (unifiedGroupSetting != null)
                                    {
                                        var props = provisioningTenant.Office365GroupsSettings.Properties;
                                        foreach (var v in unifiedGroupSetting.SettingValues)
                                        {
                                            var item = props.Where(p => p.Key == v.Name).FirstOrDefault();
                                            if (!string.IsNullOrEmpty(item.Key))
                                            {
                                                v.Value = parser.ParseString(item.Value);
                                            }
                                            else
                                            {
                                                // Set default value because null is not supported
                                                // It only accepts entire collection and not individual properties
                                                v.Value = v.DefaultValue;
                                            }
                                        }

                                        string updateDirectorySettingUrl    = $"{GraphHttpClient.GetGraphEndPointUrl(azureEnvironment)}groupSettings";
                                        var    updateDirectorySettingResult = GraphHttpClient.MakePostRequestForString(
                                            updateDirectorySettingUrl,
                                            content: new
                                        {
                                            templateId = unifiedGroupSetting.Id,
                                            values     = from v in unifiedGroupSetting.SettingValues select new { name = v.Name, value = v.Value },
                                        },
                                            contentType: "application/json",
                                            accessToken: accessToken);
                                    }
                                    else
                                    {
                                        throw new ApplicationException("Missing DirectorySettingTemplate for \"Group.Unified\"");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    scope.LogError($"Error occurred processing O365 Group settings ${ex.Message}");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        scope.LogError($"Error occurred processing O365 Group settings ${ex.Message}");
                    }
                }
            }
            return(parser);
        }