/
SyncCountry.cs
118 lines (99 loc) · 4.1 KB
/
SyncCountry.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using Microsoft.SharePoint.Client.Utilities;
using PnP.Core.Services;
using PnP.Framework;
using User = Microsoft.Graph.User;
namespace ProfileSync
{
public class SyncCountry
{
private readonly IPnPContextFactory _contextFactory;
private readonly ILogger<SyncCountry> _logger;
private readonly FunctionSettings _settings;
public SyncCountry(
IPnPContextFactory contextFactory,
FunctionSettings settings,
ILogger<SyncCountry> logger)
{
_contextFactory = contextFactory;
_settings = settings;
_logger = logger;
}
[FunctionName("SyncCountry")]
public async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo timer)
{
_logger.LogDebug("Running timer triggered 'SyncCountry' job...");
using var context = await _contextFactory.CreateAsync("Default");
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(requestMessage =>
context
.AuthenticationProvider
.AuthenticateRequestAsync(
new Uri("https://graph.microsoft.com"),
requestMessage)));
var usersCollection =
await graphClient
.Users
.Request()
.Select("id,mail,country")
.GetAsync();
do
{
await Task.WhenAll(
usersCollection
.Select(async user =>
await SyncProperties(
user)));
if (usersCollection.NextPageRequest != null)
usersCollection = await usersCollection.NextPageRequest.GetAsync();
} while (usersCollection.NextPageRequest != null);
}
private async Task<string> SyncProperties(User user)
{
using var scope = _logger.BeginScope("{SyncPropertiesId}", Guid.NewGuid());
_logger.LogDebug("Syncing user '{Mail}'", user.Mail);
try
{
using var clientContext = new AuthenticationManager()
.GetACSAppOnlyContext(
_settings.SiteUrl,
_settings.DelegateAppId,
_settings.DelegateAppSecret);
var resolvedPrincipal =
Utility.ResolvePrincipal(
clientContext,
clientContext.Web,
user.Mail,
PrincipalType.User,
PrincipalSource.All,
null,
true);
await clientContext.ExecuteQueryRetryAsync();
var person = resolvedPrincipal.Value;
var personLoginName = person.LoginName;
_logger.LogTrace("✔ Successfully resolved '{LoginName}'", personLoginName);
_logger.LogTrace("Setting UserProfile CustomCountry to '{Country}'", user.Country);
var peopleManager = new PeopleManager(clientContext);
peopleManager.SetSingleValueProfileProperty(
personLoginName,
"CustomCountry",
user.Country);
clientContext.Load(peopleManager);
await clientContext.ExecuteQueryRetryAsync();
_logger.LogInformation("✔ Successfully set UserProfile '{CustomCountry}' for '{LoginName}'", user.Country, personLoginName);
}
catch (Exception ex)
{
_logger.LogError(ex, "❌ Setting UserProfile CustomCountry failed for '{LoginName}', {Message}", user.Mail,
ex.Message);
}
return user.Mail;
}
}
}