Ejemplo n.º 1
0
        public async Task MapUrlAsync()
        {
            var services = new ServiceCollection();

            services.AddTestPnPCore();
            services.AddPnPSharePointTransformation();

            var provider = services.BuildServiceProvider();

            var pnpContextFactory = provider.GetRequiredService <IPnPContextFactory>();
            var mappingProvider   = provider.GetRequiredService <IUrlMappingProvider>();

            // Prepare contexts
            var sourceContext = new ClientContext("https://capadevtest.sharepoint.com/sites/PnPSauce");
            var targetContext = await pnpContextFactory.CreateAsync(TestCommon.TargetTestSite);

            var sourceUri     = new Uri("https://capadevtest.sharepoint.com/Documents/Folder/Employee-Handbook.docx");
            var targetPageUri = new Uri("http://site/item");

            var sourceItem = new SharePointSourceItem(sourceUri, sourceContext);

            // Prepare task
            var task    = new PageTransformationTask(new SharePointSourceProvider(sourceContext), sourceItem.Id, targetContext);
            var context = new PageTransformationContext(task, sourceItem, targetPageUri);

            // Map url
            var input  = new UrlMappingProviderInput(context, sourceUri.ToString());
            var result = await mappingProvider.MapUrlAsync(input);

            Assert.AreEqual("https://capadevtest.sharepoint.com/sites/PnPSauceModern/Documents/Folder/Employee-Handbook.docx", result.Text);
        }
        /// <summary>
        /// Maps a URL from classic to modern
        /// </summary>
        /// <param name="input">The input for the mapping activity</param>
        /// <param name="token">The cancellation token to use, if any</param>
        /// <returns>The output of the mapping activity</returns>
        public async Task <UrlMappingProviderOutput> MapUrlAsync(UrlMappingProviderInput input, CancellationToken token = default)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            logger.LogInformation(
                $"Invoked: {this.GetType().Namespace}.{this.GetType().Name}.MapUrlAsync"
                .CorrelateString(input.Context.Task.Id));

            // Try cast
            var sharePointSourceItem = input.Context.SourceItem as SharePointSourceItem;

            if (sharePointSourceItem == null)
            {
                throw new ArgumentException($"Only source item of type {typeof(SharePointSourceItem)} is supported");
            }

            string pagesLibrary = await ResolveSitePagesLibraryAsync(sharePointSourceItem.SourceContext).ConfigureAwait(false);

            sharePointSourceItem.SourceContext.Load(sharePointSourceItem.SourceContext.Web, w => w.Url);
            sharePointSourceItem.SourceContext.Load(sharePointSourceItem.SourceContext.Site, w => w.Url);
            await sharePointSourceItem.SourceContext.ExecuteQueryAsync().ConfigureAwait(false);

            // Prepare result variable
            var resultText = input?.Text ?? string.Empty;

            Uri sourceWebUrl  = new Uri(sharePointSourceItem.SourceContext.Web.Url);
            Uri sourceSiteUrl = new Uri(sharePointSourceItem.SourceContext.Site.Url);

            Uri origTargetWebUrl  = sourceWebUrl;
            Uri origSourceSiteUrl = sourceSiteUrl;
            Uri targetWebUrl      = input.Context.Task.TargetContext.Web.Url;

            bool isSubSite = origSourceSiteUrl.IsBaseOf(origTargetWebUrl);

            if (this.options.Value.UrlMappings != null && this.options.Value.UrlMappings.Count > 0)
            {
                foreach (var urlMapping in this.options.Value.UrlMappings)
                {
                    resultText = RewriteUrl(resultText,
                                            urlMapping.SourceUrl,
                                            urlMapping.TargetUrl);
                }
            }

            if (!this.options.Value.SkipUrlRewrite)
            {
                // ********************************************
                // Default URL rewriting logic
                // ********************************************
                //
                // Root site collection URL rewriting:
                // http://contoso.com/sites/portal -> https://contoso.sharepoint.com/sites/hr
                // http://contoso.com/sites/portal/pages -> https://contoso.sharepoint.com/sites/hr/sitepages
                // /sites/portal -> /sites/hr
                // /sites/portal/pages -> /sites/hr/sitepages
                //
                // If site is a sub site then we also by rewrite the sub URL's
                // http://contoso.com/sites/portal/hr -> https://contoso.sharepoint.com/sites/hr
                // http://contoso.com/sites/portal/hr/pages -> https://contoso.sharepoint.com/sites/hr/sitepages
                // /sites/portal/hr -> /sites/hr
                // /sites/portal/hr/pages -> /sites/hr/sitepages

                // Rewrite url's from pages library to sitepages
                if (!string.IsNullOrEmpty(pagesLibrary))
                {
                    var pagesSourceWebUrl     = sourceWebUrl.Combine(pagesLibrary);
                    var sitePagesTargetWebUrl = targetWebUrl.Combine("sitepages");

                    if (pagesSourceWebUrl.Scheme == "https" || pagesSourceWebUrl.Scheme == "http")
                    {
                        resultText = RewriteUrl(resultText,
                                                pagesSourceWebUrl.ToString(),
                                                sitePagesTargetWebUrl.ToString());

                        // Make relative for next replacement attempt
                        pagesSourceWebUrl     = new Uri(pagesSourceWebUrl.AbsolutePath, UriKind.Relative);
                        sitePagesTargetWebUrl = new Uri(sitePagesTargetWebUrl.AbsolutePath, UriKind.Relative);
                    }

                    resultText = RewriteUrl(resultText, pagesSourceWebUrl.ToString(), sitePagesTargetWebUrl.ToString());
                }

                // Rewrite web urls
                if (sourceWebUrl.Scheme == "https" || sourceWebUrl.Scheme == "http")
                {
                    resultText = RewriteUrl(resultText, sourceWebUrl.ToString(), targetWebUrl.ToString());
                }

                // Make relative for next replacement attempt
                resultText = RewriteUrl(resultText,
                                        sourceWebUrl.AbsolutePath.TrimEnd('/'),
                                        targetWebUrl.AbsolutePath.TrimEnd('/'));

                if (isSubSite)
                {
                    // reset URLs
                    sourceSiteUrl = origSourceSiteUrl;
                    targetWebUrl  = origTargetWebUrl;

                    // Rewrite url's from pages library to sitepages
                    if (!string.IsNullOrEmpty(pagesLibrary))
                    {
                        var pagesSourceSiteUrl    = UrlUtility.Combine(sourceSiteUrl, pagesLibrary);
                        var sitePagesTargetWebUrl = UrlUtility.Combine(targetWebUrl, "sitepages");

                        if (pagesSourceSiteUrl.Scheme == "https" || pagesSourceSiteUrl.Scheme == "http")
                        {
                            resultText = RewriteUrl(resultText,
                                                    pagesSourceSiteUrl.ToString(),
                                                    sitePagesTargetWebUrl.ToString());
                        }

                        // Make relative for next replacement attempt
                        resultText = RewriteUrl(resultText,
                                                pagesSourceSiteUrl.AbsolutePath.TrimEnd('/'),
                                                sitePagesTargetWebUrl.AbsolutePath.TrimEnd('/'));
                    }

                    // Rewrite root site urls
                    if (sourceSiteUrl.Scheme == "https" || sourceSiteUrl.Scheme == "http")
                    {
                        resultText = RewriteUrl(resultText,
                                                sourceSiteUrl.ToString(),
                                                targetWebUrl.ToString());
                    }

                    // Make relative for next replacement attempt
                    resultText = RewriteUrl(resultText,
                                            sourceSiteUrl.AbsolutePath.TrimEnd('/'),
                                            targetWebUrl.AbsolutePath.TrimEnd('/'));
                }
            }

            return(new UrlMappingProviderOutput(resultText));
        }