-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSharePointCopyOperations.cs
69 lines (68 loc) · 3.38 KB
/
SharePointCopyOperations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PnP.Core.Model.SharePoint;
using PnP.Core.Services;
class SharePointCopyOperations
{
private readonly ILogger<SharePointCopyOperations> logger;
private readonly IConfiguration config;
private readonly IPnPContextFactory factory;
private readonly AzureStorageQueueCreateCopyJobTracker copyJobTracker;
public SharePointCopyOperations(IPnPContextFactory factory, AzureStorageQueueCreateCopyJobTracker copyJobTracker, ILogger<SharePointCopyOperations> logger, IConfiguration configuration)
{
this.logger = logger;
this.config = configuration;
this.factory = factory;
this.copyJobTracker= copyJobTracker;
}
internal async Task CopyAllDriveContentsBetweenSites()
{
string rootSiteUrl = config["SourceSiteRootUrl"];
string rootSiteHostName = new Uri(rootSiteUrl).GetLeftPart(UriPartial.Authority);
logger.LogInformation(rootSiteHostName);
var context = await factory.CreateAsync(rootSiteUrl);
var web = await context.Web.GetAsync();
logger.LogInformation("Site name: {Name}", web.Title);
var folders = await GetAllFoldersAtRoot(web, config["SourceLibraryName"]);
var absoluteUrlOfSourceFolders = folders
.Where(folder => !folder.ServerRelativeUrl.EndsWith("Forms"))
.Select<IFolder, string>(folder => $"{rootSiteHostName}{folder.ServerRelativeUrl}")
.ToArray();
var infos = await context.Site.CreateCopyJobsAsync(absoluteUrlOfSourceFolders,config["DestinationLibraryAbsoluteUrl"], GetCopyMigrationOptions());
ICollection<Guid> trackingJobGuids = infos.Select(info => info.JobId).ToList();
do
{
logger.LogInformation("Going to sleep for 10 secs before checking status.");
Thread.Sleep(TimeSpan.FromSeconds(10));
//To Track the progress on each item, we need loop and pooling in there are more than 1 items in collection.
infos
.Where(info => trackingJobGuids.Contains(info.JobId))
.ToList()
.ForEach(info =>
{
//bool completed = IsJobCompleted(clientContext, info);
bool completed = copyJobTracker.IsJobCompleted(context, info);
if (completed)
{
trackingJobGuids.Remove(info.JobId);
}
});
logger.LogInformation($"Monitoring of jobs status iteration completed. pending {trackingJobGuids.Count}/{infos.Count}");
} while (trackingJobGuids.Count() != 0);
}
private static CopyMigrationOptions GetCopyMigrationOptions()
{
return new()
{
AllowSchemaMismatch = true,
IsMoveMode = false,
IgnoreVersionHistory = false,
NameConflictBehavior = SPMigrationNameConflictBehavior.Replace
};
}
internal async Task<IEnumerable<IFolder>> GetAllFoldersAtRoot(IWeb web, string libraryName)
{
IList list = await web.Lists.GetByTitleAsync(libraryName);
return list.RootFolder.Folders.AsQueryable(); // This wont work when the items cross 5000. Use Caml query
}
}