Пример #1
0
        /// <summary>
        /// Loads a Page Transformation process
        /// </summary>
        /// <param name="processId">The ID of the process to load</param>
        /// <param name="token">The cancellation token, if any</param>
        /// <returns>The transformation process</returns>
        public virtual Task <ITransformationProcess> LoadTransformationProcessAsync(Guid processId,
                                                                                    CancellationToken token = default)
        {
            // When a long running process is restored, source provider and target context are not available
            ITransformationProcess process = CreateProcess(processId);

            return(Task.FromResult(process));
        }
        /// <summary>
        /// Creates a new transformation process for SharePoint
        /// </summary>
        /// <param name="transformationProcess">The process to use</param>
        /// <param name="sourceContext">The source context</param>
        /// <param name="targetContext">The target context</param>
        /// <param name="token">The cancellation token, if any</param>
        public static Task StartSharePointProcessAsync(
            this ITransformationProcess transformationProcess,
            ClientContext sourceContext,
            PnPContext targetContext,
            CancellationToken token = default)
        {
            if (transformationProcess == null)
            {
                throw new ArgumentNullException(nameof(transformationProcess));
            }

            // Create the provider
            var sourceProvider = new SharePointSourceProvider(sourceContext);

            return(transformationProcess.StartProcessAsync(sourceProvider, targetContext, token));
        }
Пример #3
0
        /// <summary>
        /// Wait for the completion of a process
        /// </summary>
        /// <param name="process">The process to start</param>
        /// <param name="token">The cancellation token, if any</param>
        /// <returns>The status of the process</returns>
        public static async Task <TransformationProcessStatus> WaitProcessAsync(
            this ITransformationProcess process,
            CancellationToken token = default)
        {
            if (process == null)
            {
                throw new ArgumentNullException(nameof(process));
            }

            // Intercept progress
            var completionTask = new TaskCompletionSource <TransformationProcessStatus>();

            process.Progress = LocalProgress;

            // When token is cancelled, stop the process
            token.Register(() => _ = process.StopProcessAsync(token));

            // Since process could be already completed, we check immediately the status
            await LocalProgress(await process.GetStatusAsync(token).ConfigureAwait(false)).ConfigureAwait(false);

            // Wait for completion of the task
            return(await completionTask.Task.ConfigureAwait(false));

            Task LocalProgress(TransformationProcessStatus status)
            {
                switch (status.State)
                {
                case TransformationExecutionState.Completed:
                    completionTask.TrySetResult(status);
                    break;

                case TransformationExecutionState.Aborted:
                    completionTask.TrySetCanceled(token);
                    break;
                }
                return(Task.CompletedTask);
            }
        }
        /// <summary>
        /// Creates a new transformation process for SharePoint
        /// </summary>
        /// <param name="transformationProcess">The process to use</param>
        /// <param name="pnpContextFactory">The context factory to use</param>
        /// <param name="sourceContext">The source context</param>
        /// <param name="targetName">The target site name</param>
        /// <param name="token">The cancellation token, if any</param>
        /// <returns></returns>
        public static async Task StartProcessAsync(
            this ITransformationProcess transformationProcess,
            IPnPContextFactory pnpContextFactory,
            ClientContext sourceContext,
            string targetName,
            CancellationToken token = default)
        {
            if (transformationProcess == null)
            {
                throw new ArgumentNullException(nameof(transformationProcess));
            }
            if (pnpContextFactory == null)
            {
                throw new ArgumentNullException(nameof(pnpContextFactory));
            }

            // Create context
            var targetContext = await pnpContextFactory.CreateAsync(targetName).ConfigureAwait(false);

            token.ThrowIfCancellationRequested();

            await transformationProcess.StartSharePointProcessAsync(sourceContext, targetContext, token).ConfigureAwait(false);
        }
Пример #5
0
        /// <summary>
        /// Starts a process and waits for its completion
        /// </summary>
        /// <param name="process">The process to start</param>
        /// <param name="sourceProvider">The source provider to use</param>
        /// <param name="targetContext">The target PnP context</param>
        /// <param name="token">The cancellation token, if any</param>
        /// <returns></returns>
        public static async Task <TransformationProcessStatus> StartAndWaitProcessAsync(
            this ITransformationProcess process,
            ISourceProvider sourceProvider,
            PnPContext targetContext,
            CancellationToken token = default)
        {
            if (process == null)
            {
                throw new ArgumentNullException(nameof(process));
            }
            if (sourceProvider == null)
            {
                throw new ArgumentNullException(nameof(sourceProvider));
            }
            if (targetContext == null)
            {
                throw new ArgumentNullException(nameof(targetContext));
            }

            // Start the process
            await process.StartProcessAsync(sourceProvider, targetContext, token).ConfigureAwait(false);

            return(await process.WaitProcessAsync(token).ConfigureAwait(false));
        }