CSOM CreateSPAsyncReadJob保持在队列状态

时间:2019-05-17 05:40:49

标签: sharepoint-online csom

我指的是Migration Asynchronous Read API,它允许使用CSOM在SharePoint上创建读取作业。我能够成功创建读取作业,但是很遗憾,该作业长时间以来一直处于队列状态。

该函数返回Object,其中包括UniqueJobIDAzureContainerManifestUriAzureQueueReportUriEncryptionKey

通过使用clientContext.Site.GetMigrationJobStatus方法,我可以检查始终返回Queued的读取作业状态

以下是示例代码供参考:

using (var clientContext = new ClientContext(siteUrl))
{
    clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

    var result = clientContext.Site.CreateSPAsyncReadJob($"{siteUrl}/List/MyList", new AsyncReadOptions { });
    clientContext.ExecuteQuery();

    MigrationJobState state;
    do
    {
        var status = clientContext.Site.GetMigrationJobStatus(result[0].JobId);
        clientContext.ExecuteQuery();
        state = status.Value;
    } while (state == MigrationJobState.Queued);
}

我还尝试连接到包含包含加密内容的消息的AzureQueueReportUri队列。我不确定如何解密内容以使其可读。这是示例消息:

{
    "Label": "Encrypted",
    "JobId": "079ece4a-cfd2-4676-a27d-2662beb5bb0a",
    "IV": "RYc+ZA2feX1hnAcVWR1R+w==",
    "Content": "qbjTBbb2N+DkNumLoCJSAAfwj8etDLgjxp+b2T9k03L9WfRJKlFBIZO457q+CbHA+8DHJS7VbPzVMoW6ybo2GxgteTYVP+yVUOPPvz57VGQJyzg2gss+Bsjn73GTWWUfwC/W+oWnEpt8PawZysCjSNf6A4HKZKewkskCshN/pND8ZpevrGt2qq0dTt0NkTIkuYv5AvIP7DSWjdl7nN/W5x4c2nR0sPFqKYom41a4tIqrruzwCDEEjWLFtuXAQ+UN2TMV9PWabRFe9n/P1RHrAJaNU+JjJiJm+lE1dQChz+7OuQoJsYnbjYTbqEE8CnIB0/E0zTrc3zLc6th8MBsKpZJjd31ovqr/Xez6zCnvMKotSdScFtTgQqHxmVDBMfMgi2mm8cKQpdKwRufP/YhaDQlvFkmj2FQN0KAMNxwFBh/MWCVhz5uCJ50CGhChcn4h"
}

我也无法连接AzureContainerManifestUri Blob容器。它失败,并显示错误Authentication Error. Signature did not match.

有人可以指导我如何继续前进吗?

1 个答案:

答案 0 :(得分:0)

方法参数已更改。这是最新的更新文档:https://docs.microsoft.com/en-us/sharepoint/dev/apis/export-amr-api

示例代码:https://gist.github.com/techmadness/484e7de0a7c51e5faf952a79f1eacb85

using System;
using System.Linq;
using System.Security;
using System.Threading;
using Microsoft.SharePoint.Client;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var userName = "admin@tenant.onmicrosoft.com";
            var password = GetSecurePassword("password");
            var siteUrl = "https://tenant.sharepoint.com/sites/testsite";
            var listUrl = $"{siteUrl}/testlist";
            var azStorageConnectionStrong = "DefaultEndpointsProtocol=https;AccountName=abcd;AccountKey=xyz";

            using (var clientContext = new ClientContext(siteUrl))
            {
                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

                var azManifestContainer = CreateContainerIfNotExists(azStorageConnectionStrong, "spread-manifest-container");
                var azReportQueue = CreateQueueIfNotExists(azStorageConnectionStrong, "spread-report-queue");

                var azManifestContainerUrl = GetSASUrl(azManifestContainer);
                var azReportQueueUrl = GetSASUrl(azReportQueue);

                var output = clientContext.Site.CreateSPAsyncReadJob(
                    listUrl,
                    new AsyncReadOptions
                    {
                        IncludeDirectDescendantsOnly = true,
                        IncludeSecurity = true,
                    },
                    null,
                    azManifestContainerUrl,
                    azReportQueueUrl);
                clientContext.ExecuteQuery();

                CloudQueueMessage message;
                do
                {
                    Thread.Sleep(TimeSpan.FromSeconds(10));
                    message = azReportQueue.GetMessage();
                    if (message != null)
                    {
                        Console.WriteLine(message.AsString);
                        azReportQueue.DeleteMessage(message);

                    }
                } while (message != null);

                Console.ReadLine();
            }
        }

        private static SecureString GetSecurePassword(string pwd)
        {
            SecureString securePassword = new SecureString();
            foreach (var ch in pwd.ToArray())
            {
                securePassword.AppendChar(ch);
            }
            return securePassword;
        }

        private static CloudBlobContainer CreateContainerIfNotExists(string storageConnectionString, string containerName)
        {
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(containerName);
            container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
            return container;
        }

        private static CloudQueue CreateQueueIfNotExists(string storageConnectionString, string queueName)
        {
            var cloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString);
            var queueClient = cloudStorageAccount.CreateCloudQueueClient();
            var queue = queueClient.GetQueueReference(queueName);
            queue.CreateIfNotExistsAsync().GetAwaiter().GetResult();
            return queue;
        }

        public static string GetSASUrl(CloudBlobContainer container)
        {
            var sharedAccessSignature = container.GetSharedAccessSignature(new SharedAccessBlobPolicy
            {
                Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write,
                SharedAccessStartTime = DateTime.UtcNow.AddDays(-1),
                SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7),
            });
            return container.StorageUri.PrimaryUri + sharedAccessSignature;
        }

        public static string GetSASUrl(CloudQueue queue)
        {
            var sharedAccessSignature = queue.GetSharedAccessSignature(new SharedAccessQueuePolicy
            {
                Permissions = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.Read,
                SharedAccessStartTime = DateTime.UtcNow.AddDays(-1),
                SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7)
            });
            return queue.StorageUri.PrimaryUri + sharedAccessSignature;
        }
    }
}