public static IServiceProvider AddTestPnPCore(this IServiceCollection services) { var configuration = GetConfigurationSettings(); services // Configuration .AddScoped <IConfiguration>(_ => configuration) // Logging service, get config from appsettings + add debug output handler .AddLogging(configure => { configure.AddConfiguration(configuration.GetSection("Logging")); configure.AddDebug(); }) // Add the PnP Core SDK library services configuration from the appsettings.json file .Configure <PnPCoreOptions>(configuration.GetSection("PnPCore")) .Configure <PnPCoreAuthenticationOptions>(configuration.GetSection("PnPCore")) .AddPnPCoreAuthentication() // Add the PnP Core SDK Authentication Providers .AddPnPCore(); // The default configuration has to use credential manager for auth var defaultConfiguration = configuration.GetSection("PnPCore:Credentials:DefaultConfiguration")?.Value; var clientId = configuration.GetSection($"PnPCore:Credentials:Configurations:{defaultConfiguration}:ClientId")?.Value; var tenantId = configuration.GetSection($"PnPCore:Credentials:Configurations:{defaultConfiguration}:TenantId")?.Value; var credentialManager = configuration.GetSection($"PnPCore:Credentials:Configurations:{defaultConfiguration}:CredentialManager:CredentialManagerName")?.Value; var resource = $"https://{new Uri(configuration["SourceTestSite"]).Authority}"; var cmap = new CredentialManagerAuthenticationProvider(clientId, tenantId, credentialManager); var accessToken = cmap.GetAccessTokenAsync(new Uri(resource)).GetAwaiter().GetResult(); services.AddTransient(p => { var clientContext = new ClientContext(configuration["SourceTestSite"]); clientContext.ExecutingWebRequest += (sender, args) => { if (cmap != null) { args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken; } }; return(clientContext); }); return(services.BuildServiceProvider()); }
static async Task Main(string[] args) { #region Dependency Injection plumbing var host = Host.CreateDefaultBuilder() .ConfigureLogging((hostingContext, logging) => { logging.AddEventSourceLogger(); logging.AddConsole(); }) .ConfigureServices((hostingContext, services) => { // Register the PnP Core services services.AddPnPCore(); services.Configure <PnPCoreOptions>(hostingContext.Configuration.GetSection("PnPCore")); // Register the PnP Core authentication services services.AddPnPCoreAuthentication(); services.Configure <PnPCoreAuthenticationOptions>(hostingContext.Configuration.GetSection("PnPCore")); // Register the PnP Transformation Framework services with SharePoint as a data source services.AddPnPSharePointTransformation(null, spOptions => { //spOptions.WebPartMappingFile = @"C:\github\pnpcore\src\sdk\PnP.Core.Transformation.SharePoint\MappingFiles\webpartmapping.xml"; //spOptions.PageLayoutMappingFile = @"C:\github\pnpcore\src\sdk\PnP.Core.Transformation.SharePoint\MappingFiles\pagelayoutmapping.xml"; spOptions.CopyPageMetadata = true; spOptions.KeepPageSpecificPermissions = true; spOptions.RemoveEmptySectionsAndColumns = true; spOptions.ShouldMapUsers = true; spOptions.TargetPageTakesSourcePageName = true; }); // Register the CSOM ClientContext for the data source services.AddTransient(p => { var clientContext = new ClientContext(hostingContext.Configuration["SourceSite"]); clientContext.ExecutingWebRequest += (sender, args) => { var resource = $"https://{new Uri(hostingContext.Configuration["SourceSite"]).Authority}"; var clientId = hostingContext.Configuration.GetSection("PnPCore:Credentials:Configurations:CredentialManager:ClientId")?.Value; var tenantId = hostingContext.Configuration.GetSection("PnPCore:Credentials:Configurations:CredentialManager:TenantId")?.Value; var credentialManager = hostingContext.Configuration.GetSection("PnPCore:Credentials:Configurations:CredentialManager:CredentialManager:CredentialManagerName")?.Value; var cmap = new CredentialManagerAuthenticationProvider(clientId, tenantId, credentialManager); if (cmap != null) { args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + cmap.GetAccessTokenAsync(new Uri(resource)).GetAwaiter().GetResult(); } }; return(clientContext); }); }) // Let the builder know we're running in a console .UseConsoleLifetime() // Add services to the container .Build(); await host.StartAsync(); #endregion using (var scope = host.Services.CreateScope()) { var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>(); var pnpContextFactory = scope.ServiceProvider.GetRequiredService <IPnPContextFactory>(); var pageTransformator = scope.ServiceProvider.GetRequiredService <IPageTransformator>(); var sourceContext = scope.ServiceProvider.GetRequiredService <ClientContext>(); using (var targetContext = await pnpContextFactory.CreateAsync("TargetSite")) { var sourcePageUri = configuration.GetValue <string>("SourcePageUri"); var sourceUri = new Uri(sourcePageUri); var result = await pageTransformator.TransformSharePointAsync(sourceContext, targetContext, sourceUri); Console.WriteLine($"Here is the URL of the transformed page: {result}"); } } }
public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddSingleton(p => CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"))); builder.Services.AddSingleton(p => { var account = p.GetRequiredService <CloudStorageAccount>(); return(account.CreateCloudTableClient()); }); builder.Services.AddSingleton(p => { var account = p.GetRequiredService <CloudStorageAccount>(); return(account.CreateCloudQueueClient()); }); // Configure PnP Core and PnP Core Auth options builder.Services.AddOptions <PnPCoreOptions>() .Configure <IConfiguration>((options, configuration) => configuration.GetSection("PnPCore").Bind(options)); builder.Services.AddOptions <PnPCoreAuthenticationOptions>() .Configure <IConfiguration>((options, configuration) => configuration.GetSection("PnPCore").Bind(options)); // Register PnP Core and PnP Core Auth services builder.Services.AddPnPCoreAuthentication(); builder.Services.AddPnPCore(); // Register the PnP Core Auth providers // Workaround: DryIoc (used by functions choose wrong ctor) builder.Services.RemoveAll <X509CertificateAuthenticationProvider>(); builder.Services.RemoveAll <CredentialManagerAuthenticationProvider>(); builder.Services.AddTransient(p => new CredentialManagerAuthenticationProvider( p.GetRequiredService <ILogger <OAuthAuthenticationProvider> >())); builder.Services.AddTransient(p => new X509CertificateAuthenticationProvider( p.GetRequiredService <ILogger <OAuthAuthenticationProvider> >())); // Register the PnP Transformation Framework services // for a SharePoint to SharePoint transformation builder.Services.AddPnPSharePointTransformation() .WithTransformationStateManager <AzureTableTransformationStateManager>() .WithTransformationExecutor <AzureQueueTransformationExecutor>(); // Register the CSOM ClientContext for the data source builder.Services.AddTransient(p => { var clientContext = new ClientContext(Environment.GetEnvironmentVariable("SourceSite")); clientContext.ExecutingWebRequest += (sender, args) => { var resource = $"https://{new Uri(Environment.GetEnvironmentVariable("SourceSite")).Authority}"; var clientId = Environment.GetEnvironmentVariable("PnPCore:Credentials:Configurations:CredentialManager:ClientId"); var tenantId = Environment.GetEnvironmentVariable("PnPCore:Credentials:Configurations:CredentialManager:TenantId"); var credentialManager = Environment.GetEnvironmentVariable("PnPCore:Credentials:Configurations:CredentialManager:CredentialManager:CredentialManagerName"); var cmap = new CredentialManagerAuthenticationProvider(clientId, tenantId, credentialManager); if (cmap != null) { args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + cmap.GetAccessTokenAsync(new Uri(resource)).GetAwaiter().GetResult(); } }; return(clientContext); }); }