Exemplo n.º 1
0
        internal string GetFieldValue(BaseTransformationInformation baseTransformationInformation, string fieldName)
        {
            if (baseTransformationInformation.SourcePage != null)
            {
                return(baseTransformationInformation.SourcePage[fieldName].ToString());
            }
            else
            {
                if (baseTransformationInformation.SourceFile != null)
                {
                    var fileServerRelativeUrl = baseTransformationInformation.SourceFile.EnsureProperty(p => p.ServerRelativeUrl);

                    // come up with equivalent field values for the page without listitem (so page living in the root folder of the site)
                    if (fieldName.Equals(Constants.FileRefField))
                    {
                        // e.g. /sites/espctest2/SitePages/demo16.aspx
                        return(fileServerRelativeUrl);
                    }
                    else if (fieldName.Equals(Constants.FileDirRefField))
                    {
                        // e.g. /sites/espctest2/SitePages
                        return(fileServerRelativeUrl.Replace($"/{System.IO.Path.GetFileName(fileServerRelativeUrl)}", ""));
                    }
                    else if (fieldName.Equals(Constants.FileLeafRefField))
                    {
                        // e.g. demo16.aspx
                        return(System.IO.Path.GetFileName(fileServerRelativeUrl));
                    }
                }
                return("");
            }
        }
Exemplo n.º 2
0
 internal bool FieldExistsAndIsUsed(BaseTransformationInformation baseTransformationInformation, string fieldName)
 {
     if (baseTransformationInformation.SourcePage != null)
     {
         return(baseTransformationInformation.SourcePage.FieldExistsAndUsed(fieldName));
     }
     else
     {
         return(true);
     }
 }
        public UrlTransformator(BaseTransformationInformation baseTransformationInformation, ClientContext sourceContext, ClientContext targetContext, IList <ILogObserver> logObservers = null)
        {
            // Hookup logging
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            // Ensure source and target context are set
            if (sourceContext == null && targetContext != null)
            {
                sourceContext = targetContext;
            }

            if (targetContext == null && sourceContext != null)
            {
                targetContext = sourceContext;
            }

            // Grab the needed information to drive url rewrite
            this.sourceContext = sourceContext;
            this.targetContext = targetContext;

            sourceContext.Site.EnsureProperties(p => p.ServerRelativeUrl, p => p.Url);

            this.sourceSiteUrl = sourceContext.Site.Url;
            this.sourceWebUrl  = sourceContext.Web.GetUrl();

            //TODO: Check Is this always pages and sitePages part of the URL NOT title?
            if (sourceContext.Web.IsPublishingWeb())
            {
                this.pagesLibrary = CacheManager.Instance.GetPublishingPagesLibraryName(this.sourceContext);
            }
            else
            {
                this.pagesLibrary = "sitepages";
            }


            this.targetWebUrl = targetContext.Web.GetUrl();
            // Load the URL mapping file
            if (!string.IsNullOrEmpty(baseTransformationInformation?.UrlMappingFile))
            {
                this.urlMapping = CacheManager.Instance.GetUrlMapping(baseTransformationInformation.UrlMappingFile, logObservers);
            }

            this.skipDefaultUrlRewrite = baseTransformationInformation.SkipDefaultUrlRewrite;
        }
Exemplo n.º 4
0
        /// <summary>
        /// User Transformator constructor
        /// </summary>
        /// <param name="baseTransformationInformation">Transformation configuration settings</param>
        /// <param name="sourceContext">Source Context</param>
        /// <param name="targetContext">Target Context</param>
        /// <param name="logObservers">Connected loggers</param>
        public UserTransformator(BaseTransformationInformation baseTransformationInformation, ClientContext sourceContext, ClientContext targetContext, IList <ILogObserver> logObservers = null)
        {
            // Hookup logging
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            // Ensure source and target context are set
            if (sourceContext == null && targetContext != null)
            {
                sourceContext = targetContext;
            }

            if (targetContext == null && sourceContext != null)
            {
                targetContext = sourceContext;
            }

            this._sourceContext = sourceContext;
            this._targetContext = targetContext;

            // Load the User mapping file
            if (!string.IsNullOrEmpty(baseTransformationInformation?.UserMappingFile))
            {
                this._userMapping = CacheManager.Instance.GetUserMapping(baseTransformationInformation.UserMappingFile, logObservers);
            }
            else
            {
                this._userMapping = default; //Pass through if there is no mapping
            }

            if (!string.IsNullOrEmpty(baseTransformationInformation.LDAPConnectionString))
            {
                _ldapSpecifiedByUser = baseTransformationInformation?.LDAPConnectionString;
            }
            else
            {
                _ldapSpecifiedByUser = default;
            }

            _sourceVersion   = baseTransformationInformation?.SourceVersion ?? SPVersion.SPO; // SPO Fall Back
            _skipUserMapping = baseTransformationInformation.SkipUserMapping;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Validates settings when doing a cross farm transformation
        /// </summary>
        /// <param name="baseTransformationInformation">Transformation Information</param>
        /// <remarks>Will disable feature if not supported</remarks>
        internal void CrossFarmTransformationValidation(BaseTransformationInformation baseTransformationInformation)
        {
            // Source only context - allow item level permissions
            // Source to target same base address - allow item level permissions
            // Source to target difference base address - disallow item level permissions

            if (targetClientContext != null && sourceClientContext != null)
            {
                if (!sourceClientContext.Url.Equals(targetClientContext.Url, StringComparison.InvariantCultureIgnoreCase))
                {
                    baseTransformationInformation.IsCrossSiteTransformation = true;
                }

                var sourceUrl = sourceClientContext.Url.GetBaseUrl();
                var targetUrl = targetClientContext.Url.GetBaseUrl();

                // Override the setting for keeping item level permissions
                if (!sourceUrl.Equals(targetUrl, StringComparison.InvariantCultureIgnoreCase))
                {
                    baseTransformationInformation.KeepPageSpecificPermissions = false;
                    LogWarning(LogStrings.Warning_ContextValidationFailWithKeepPermissionsEnabled, LogStrings.Heading_InputValidation);

                    // Set a global flag to indicate this is a cross farm transformation (on-prem to SPO tenant or SPO Tenant A to SPO Tenant B)
                    baseTransformationInformation.IsCrossFarmTransformation = true;
                }
            }

            if (sourceClientContext != null)
            {
                baseTransformationInformation.SourceVersion       = GetVersion(sourceClientContext);
                baseTransformationInformation.SourceVersionNumber = GetExactVersion(sourceClientContext);
            }

            if (targetClientContext != null)
            {
                baseTransformationInformation.TargetVersion       = GetVersion(targetClientContext);
                baseTransformationInformation.TargetVersionNumber = GetExactVersion(targetClientContext);
            }

            if (sourceClientContext != null && targetClientContext == null)
            {
                baseTransformationInformation.TargetVersion       = baseTransformationInformation.SourceVersion;
                baseTransformationInformation.TargetVersionNumber = baseTransformationInformation.SourceVersionNumber;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Ensures that the contexts are the same for the feature to be supported
        /// </summary>
        /// <param name="baseTransformationInformation">Transformation Information</param>
        /// <remarks>Will disable feature if not supported</remarks>
        internal void EnsureItemLevelPermissionsContextsSupported(BaseTransformationInformation baseTransformationInformation)
        {
            // Source only context - allow item level permissions
            // Source to target same base address - allow item level permissions
            // Source to target difference base address - disallow item level permissions

            if (targetClientContext != null && sourceClientContext != null && baseTransformationInformation.KeepPageSpecificPermissions)
            {
                var sourceUrl = sourceClientContext.Url.GetBaseUrl();
                var targetUrl = targetClientContext.Url.GetBaseUrl();

                // Override the setting for keeping item level permissions
                if (!sourceUrl.Equals(targetUrl, StringComparison.InvariantCultureIgnoreCase))
                {
                    baseTransformationInformation.KeepPageSpecificPermissions = false;
                    LogWarning(LogStrings.Warning_ContextValidationFailWithKeepPermissionsEnabled, LogStrings.Heading_InputValidation);
                }
            }
        }
        /// <summary>
        /// Instantiates the content transformator
        /// </summary>
        /// <param name="page">Client side page that will be updates</param>
        /// <param name="pageTransformation">Transformation information</param>
        public ContentTransformator(ClientContext sourceClientContext, ClientSidePage page, PageTransformation pageTransformation, BaseTransformationInformation transformationInformation, IList <ILogObserver> logObservers = null) : base()
        {
            //Register any existing observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.page = page ?? throw new ArgumentException("Page cannot be null");
            this.pageTransformation        = pageTransformation ?? throw new ArgumentException("pageTransformation cannot be null");
            this.globalTokens              = CreateGlobalTokenList(page.Context, transformationInformation.MappingProperties);
            this.functionProcessor         = new FunctionProcessor(sourceClientContext, this.page, this.pageTransformation, transformationInformation, base.RegisteredLogObservers);
            this.transformationInformation = transformationInformation;

            this.sourceClientContext = sourceClientContext;
            this.isCrossSiteTransfer = IsCrossSiteTransfer();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructor for the Term Transformator class
        /// </summary>
        /// <param name="baseTransformationInformation"></param>
        /// <param name="sourceContext"></param>
        /// <param name="targetContext"></param>
        /// <param name="logObservers"></param>
        public TermTransformator(BaseTransformationInformation baseTransformationInformation, ClientContext sourceContext, ClientContext targetContext, IList <ILogObserver> logObservers = null)
        {
            // Hookup logging
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            // Ensure source and target context are set
            if (sourceContext == null && targetContext != null)
            {
                sourceContext = targetContext;
            }

            if (targetContext == null && sourceContext != null)
            {
                targetContext = sourceContext;
            }

            this._sourceContext = sourceContext;
            this._targetContext = targetContext;

            // Load the Term mapping file
            if (!string.IsNullOrEmpty(baseTransformationInformation?.TermMappingFile))
            {
                this.termMappings = CacheManager.Instance.GetTermMapping(baseTransformationInformation.TermMappingFile, logObservers);
            }

            if (baseTransformationInformation != null)
            {
                this.skipTermStoreMapping           = baseTransformationInformation.SkipTermStoreMapping;
                this._baseTransformationInformation = baseTransformationInformation;
            }
        }
        public UrlTransformator(BaseTransformationInformation baseTransformationInformation, ClientContext sourceContext, ClientContext targetContext, IList <ILogObserver> logObservers = null)
        {
            // Hookup logging
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            // Ensure source and target context are set
            if (sourceContext == null && targetContext != null)
            {
                sourceContext = targetContext;
            }

            if (targetContext == null && sourceContext != null)
            {
                targetContext = sourceContext;
            }

            // Grab the needed information to drive url rewrite
            this.sourceContext = sourceContext;
            this.targetContext = targetContext;
            this.sourceSiteUrl = sourceContext.Site.EnsureProperty(p => p.Url);
            this.sourceWebUrl  = sourceContext.Web.EnsureProperty(p => p.Url);
            this.pagesLibrary  = CacheManager.Instance.GetPublishingPagesLibraryName(this.sourceContext);
            this.targetWebUrl  = targetContext.Web.EnsureProperty(p => p.Url);

            // Load the URL mapping file
            if (!string.IsNullOrEmpty(baseTransformationInformation.UrlMappingFile))
            {
                this.urlMapping = CacheManager.Instance.GetUrlMapping(baseTransformationInformation.UrlMappingFile, logObservers);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Default constructor
        /// </summary>
        public WikiHtmlTransformator(ClientContext sourceContext, ClientSidePage page, BaseTransformationInformation basePageTransformationInformation, IList <ILogObserver> logObservers = null)
        {
            //Register any existing observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.sourceContext     = sourceContext;
            this.page              = page;
            this.mappingProperties = basePageTransformationInformation.MappingProperties;

            // Instantiate BuiltIn functions class
            this.builtInFunctions = new BuiltIn(basePageTransformationInformation, this.page.Context, this.sourceContext, this.page, base.RegisteredLogObservers);

            // Instantiate the AngleSharp Html parser
            parser = new HtmlParser(new HtmlParserOptions()
            {
                IsEmbedded = true
            });
        }
Exemplo n.º 11
0
        internal void UpdateTargetPageWithSourcePageInformation(ListItem targetPage, BaseTransformationInformation baseTransformationInformation, string serverRelativePathForModernPage, bool crossSiteTransformation)
        {
            try
            {
                FieldUserValue pageAuthor = this.SourcePageAuthor;
                FieldUserValue pageEditor = this.SourcePageEditor;

                // Keeping page author information is only possible when staying in SPO...for cross site support we first do need user account mapping
                var sourcePlatformVersion = baseTransformationInformation.SourceVersion;

                if (crossSiteTransformation && baseTransformationInformation.KeepPageCreationModificationInformation && sourcePlatformVersion == SPVersion.SPO)
                {
                    // If transformtion is cross site collection we'll need to lookup users again
                    // Using a cloned context to not mess up with the pending list item updates
                    using (var clonedTargetContext = targetClientContext.Clone(targetClientContext.Web.GetUrl()))
                    {
                        var pageAuthorUser = clonedTargetContext.Web.EnsureUser(this.SourcePageAuthor.LookupValue);
                        var pageEditorUser = clonedTargetContext.Web.EnsureUser(this.SourcePageEditor.LookupValue);
                        clonedTargetContext.Load(pageAuthorUser);
                        clonedTargetContext.Load(pageEditorUser);
                        clonedTargetContext.ExecuteQueryRetry();

                        // Prep a new FieldUserValue object instance and update the list item
                        pageAuthor = new FieldUserValue()
                        {
                            LookupId = pageAuthorUser.Id
                        };

                        pageEditor = new FieldUserValue()
                        {
                            LookupId = pageEditorUser.Id
                        };
                    }
                }

                if (baseTransformationInformation.KeepPageCreationModificationInformation || baseTransformationInformation.PostAsNews)
                {
                    if (baseTransformationInformation.KeepPageCreationModificationInformation && sourcePlatformVersion == SPVersion.SPO)
                    {
                        // All 4 fields have to be set!
                        targetPage[Constants.CreatedByField]  = pageAuthor;
                        targetPage[Constants.ModifiedByField] = pageEditor;
                        targetPage[Constants.CreatedField]    = this.SourcePageCreated;
                        targetPage[Constants.ModifiedField]   = this.SourcePageModified;
                    }

                    if (baseTransformationInformation.PostAsNews)
                    {
                        targetPage[Constants.PromotedStateField] = "2";

                        // Determine what will be the publishing date that will show up in the news rollup
                        if (baseTransformationInformation.KeepPageCreationModificationInformation && sourcePlatformVersion == SPVersion.SPO)
                        {
                            targetPage[Constants.FirstPublishedDateField] = this.SourcePageModified;
                        }
                        else
                        {
                            targetPage[Constants.FirstPublishedDateField] = targetPage[Constants.ModifiedField];
                        }
                    }

                    targetPage.UpdateOverwriteVersion();

                    if (baseTransformationInformation.PublishCreatedPage)
                    {
                        var targetPageFile = ((targetPage.Context) as ClientContext).Web.GetFileByServerRelativeUrl(serverRelativePathForModernPage);
                        targetPage.Context.Load(targetPageFile);
                        // Try to publish, if publish is not needed/possible (e.g. when no minor/major versioning set) then this will return an error that we'll be ignoring
                        targetPageFile.Publish(LogStrings.PublishMessage);
                    }
                }

                targetPage.Context.ExecuteQueryRetry();
            }
            catch (Exception ex)
            {
                // Eat exceptions as this is not critical for the generated page
                LogWarning(LogStrings.Warning_NonCriticalErrorDuringPublish, LogStrings.Heading_ArticlePageHandling);
            }
        }