public async Task Apply_GeneratesCorrectRequest()
        {
            // ARRANGE
            var mockRequestData = new ApplySiteDesignRequest
            {
                SiteDesignId = "mockSiteDesignId",
                WebUrl       = mockWebUrl
            };
            var expectedUri     = new Uri($"{mockWebUrl}/_api/Microsoft.Sharepoint.Utilities.WebTemplateExtensions.SiteScriptUtility.ApplySiteDesign");
            var expectedContent = $"{{\"siteDesignId\":\"mockSiteDesignId\",\"webUrl\":\"{mockWebUrl.ToString()}\"}}";

            using (var response = new HttpResponseMessage())
                using (var gsc = GraphServiceTestClient.Create(response))
                {
                    // ACT
                    _ = await gsc.GraphServiceClient
                        .SharePointAPI(mockWebUrl)
                        .SiteDesigns
                        .Request()
                        .ApplyAsync(mockRequestData);

                    var actualContent = gsc.HttpProvider.ContentAsString;

                    // ASSERT
                    gsc.HttpProvider.Verify(
                        provider => provider.SendAsync(
                            It.Is <HttpRequestMessage>(req =>
                                                       req.Method == HttpMethod.Post &&
                                                       req.RequestUri == expectedUri &&
                                                       req.Headers.Authorization != null
                                                       ),
                            It.IsAny <HttpCompletionOption>(),
                            It.IsAny <CancellationToken>()
                            ),
                        Times.Exactly(1)
                        );
                    Assert.Equal(Microsoft.Graph.CoreConstants.MimeTypeNames.Application.Json, gsc.HttpProvider.ContentHeaders.ContentType.MediaType);
                    Assert.Equal(expectedContent, actualContent);
                }
        }
        public async Task Apply_ReturnsCorrectResponse()
        {
            // ARRANGE
            var mockRequestData = new ApplySiteDesignRequest
            {
                SiteDesignId = "mockSiteDesignId",
                WebUrl       = mockWebUrl
            };
            var responseContent = ResourceManager.GetHttpResponseContent("ApplySiteDesignResponse.json");
            var responseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(responseContent),
            };

            using (responseMessage)
                using (var gsc = GraphServiceTestClient.Create(responseMessage))
                {
                    // ACT
                    var response = await gsc.GraphServiceClient
                                   .SharePointAPI(mockWebUrl)
                                   .SiteDesigns
                                   .Request()
                                   .ApplyAsync(mockRequestData);

                    var actual = response;

                    // ASSERT
                    Assert.Equal(6, actual.ActionOutcomes.Count);
                    Assert.Equal(SiteScriptActionOutcome.Success, actual.ActionOutcomes[1].Outcome);
                    Assert.Equal("Create site column Project name", actual.ActionOutcomes[1].Title);
                    Assert.Null(actual.ActionOutcomes[1].OutcomeText);

                    Assert.Equal(SiteScriptActionOutcome.NoOp, actual.ActionOutcomes[2].Outcome);
                    Assert.Equal("Create content type Customer", actual.ActionOutcomes[2].Title);
                    Assert.Null(actual.ActionOutcomes[2].OutcomeText);
                }
        }
        public static async Task Run()
        {
            /////////////////////////////
            //
            // Programmer configuration
            //
            /////////////////////////////

            var sharepointDomain   = "demo.sharepoint.com";
            var siteCollectionPath = "/sites/SiteDesignTest";

            ////////////////////////////////
            //
            // Azure AD Configuration
            //
            ////////////////////////////////

            AzureAdOptions azureAdOptions = new AzureAdOptions();

            var settingsFilename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "appsettings.json");
            var builder          = new ConfigurationBuilder()
                                   .AddJsonFile(settingsFilename, optional: false)
                                   .AddUserSecrets <Program>();
            var config = builder.Build();

            config.Bind("AzureAd", azureAdOptions);

            /////////////////////////////////////
            //
            // Client Application Configuration
            //
            /////////////////////////////////////

            var options = new PublicClientApplicationOptions()
            {
                AadAuthorityAudience = AadAuthorityAudience.AzureAdMyOrg,
                AzureCloudInstance   = AzureCloudInstance.AzurePublic,
                ClientId             = azureAdOptions.ClientId,
                TenantId             = azureAdOptions.TenantId,
                RedirectUri          = "http://localhost"
            };

            // Create the public client application (desktop app), with a default redirect URI
            var pca = PublicClientApplicationBuilder
                      .CreateWithApplicationOptions(options)
                      .Build();

            // Enable a simple token cache serialiation so that the user does not need to
            // re-sign-in each time the application is run
            TokenCacheHelper.EnableSerialization(pca.UserTokenCache);

            ///////////////////////////////////////////////
            //
            //  Auth Provider - Device Code in this sample
            //
            ///////////////////////////////////////////////

            // Create an authentication provider to attach the token to requests
            var scopes = new string[] { $"https://{sharepointDomain}/AllSites.FullControl" };
            IAuthenticationProvider ap = new DeviceCodeProvider(pca, scopes);

            ////////////////////////////////////////////////////////////
            //
            // Graph Client with Logger and SharePoint service handler
            //
            ////////////////////////////////////////////////////////////

            var logger = new StringBuilderHttpMessageLogger();

            /*
             *  Could also use the Console if preferred...
             *
             *  var logger = new ConsoleHttpMessageLogger();
             */

            // Configure our client
            CommunityGraphClientOptions clientOptions = new CommunityGraphClientOptions()
            {
                UserAgent = "SiteDesignSample"
            };
            var graphServiceClient = CommunityGraphClientFactory.Create(clientOptions, logger, ap);

            ///////////////////////////////////////
            //
            // Setup is complete, run the sample
            //
            ///////////////////////////////////////

            var WebUrl = $"https://{sharepointDomain}{siteCollectionPath}";

            var siteScript = new SiteScriptMetadata()
            {
                Title       = "Green Theme",
                Description = "Apply the Green Theme",
                Content     = "{\"$schema\": \"schema.json\",\"actions\": [{\"verb\": \"applyTheme\",\"themeName\": \"Green\"}],\"bindata\": { },\"version\": 1}",
            };

            var createdScript = await graphServiceClient
                                .SharePointAPI(WebUrl)
                                .SiteScripts
                                .Request()
                                .CreateAsync(siteScript);

            var siteDesign = new SiteDesignMetadata()
            {
                Title         = "Green Theme",
                Description   = "Apply the Green theme",
                SiteScriptIds = new System.Collections.Generic.List <Guid>()
                {
                    new Guid(createdScript.Id)
                },
                WebTemplate = "64"                 // 64 = Team Site, 68 = Communication Site, 1 = Groupless Team Site
            };

            var createdDesign = await graphServiceClient
                                .SharePointAPI(WebUrl)
                                .SiteDesigns
                                .Request()
                                .CreateAsync(siteDesign);

            var applySiteDesignRequest = new ApplySiteDesignRequest
            {
                SiteDesignId = createdDesign.Id,
                WebUrl       = WebUrl
            };

            var applySiteDesignResponse = await graphServiceClient
                                          .SharePointAPI(WebUrl)
                                          .SiteDesigns.Request()
                                          .ApplyAsync(applySiteDesignRequest);

            foreach (var outcome in applySiteDesignResponse.ActionOutcomes)
            {
                Console.WriteLine(outcome.OutcomeText);
            }


            Console.WriteLine("Press enter to show log");
            Console.ReadLine();
            Console.WriteLine();
            var log = logger.GetLog();

            Console.WriteLine(log);
        }
        public static async Task Run()
        {
            /////////////////////////////
            //
            // Programmer configuration
            //
            /////////////////////////////

            var sharepointDomain   = "demo.sharepoint.com";
            var siteCollectionPath = "/sites/SiteDesignTest";

            /////////////////
            //
            // Configuration
            //
            /////////////////

            AzureAdOptions azureAdOptions = new AzureAdOptions();

            var settingsFilename = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "appsettings.json");
            var builder          = new ConfigurationBuilder()
                                   .AddJsonFile(settingsFilename, optional: false)
                                   .AddUserSecrets <Program>();
            var config = builder.Build();

            config.Bind("AzureAd", azureAdOptions);

            ////////////////////////////
            //
            // Graph Client with Logger
            //
            ////////////////////////////

            var logger = new StringBuilderHttpMessageLogger();

            /*
             *  Could also use the Console if preferred...
             *
             *  var logger = new ConsoleHttpMessageLogger();
             */

            var pca = PublicClientApplicationBuilder
                      .Create(azureAdOptions.ClientId)
                      .WithTenantId(azureAdOptions.TenantId)
                      .Build();

            var scopes = new string[] { $"https://{sharepointDomain}/AllSites.FullControl" };
            IAuthenticationProvider ap = new DeviceCodeProvider(pca, scopes);

            using (LoggingMessageHandler loggingHandler = new LoggingMessageHandler(logger))
                using (HttpProvider hp = new HttpProvider(loggingHandler, false, new Serializer()))
                {
                    GraphServiceClient graphServiceClient = new GraphServiceClient(ap, hp);

                    ////////////////////////////
                    //
                    // Setup is complete, run the sample
                    //
                    ////////////////////////////

                    var WebUrl = $"https://{sharepointDomain}{siteCollectionPath}";

                    var siteScript = new SiteScriptMetadata()
                    {
                        Title       = "Green Theme",
                        Description = "Apply the Green Theme",
                        Content     = "{\"$schema\": \"schema.json\",\"actions\": [{\"verb\": \"applyTheme\",\"themeName\": \"Green\"}],\"bindata\": { },\"version\": 1}",
                    };

                    var createdScript = await graphServiceClient
                                        .SharePointAPI(WebUrl)
                                        .SiteScripts
                                        .Request()
                                        .CreateAsync(siteScript);

                    var siteDesign = new SiteDesignMetadata()
                    {
                        Title         = "Green Theme",
                        Description   = "Apply the Green theme",
                        SiteScriptIds = new System.Collections.Generic.List <Guid>()
                        {
                            new Guid(createdScript.Id)
                        },
                        WebTemplate = "64"                 // 64 = Team Site, 68 = Communication Site, 1 = Groupless Team Site
                    };

                    var createdDesign = await graphServiceClient
                                        .SharePointAPI(WebUrl)
                                        .SiteDesigns
                                        .Request()
                                        .CreateAsync(siteDesign);

                    var applySiteDesignRequest = new ApplySiteDesignRequest
                    {
                        SiteDesignId = createdDesign.Id,
                        WebUrl       = WebUrl
                    };

                    var applySiteDesignResponse = await graphServiceClient
                                                  .SharePointAPI(WebUrl)
                                                  .SiteDesigns.Request()
                                                  .ApplyAsync(applySiteDesignRequest);

                    foreach (var outcome in applySiteDesignResponse.ActionOutcomes)
                    {
                        Console.WriteLine(outcome.OutcomeText);
                    }


                    Console.WriteLine("Press enter to show log");
                    Console.ReadLine();
                    Console.WriteLine();
                    var log = logger.GetLog();
                    Console.WriteLine(log);
                }
        }