This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Program.cs
170 lines (144 loc) · 6.73 KB
/
Program.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Core.UserProfiles.Sync.XMLObjects;
using GraphApi = Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Linq;
using System.Linq.Expressions;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Serialization;
using Microsoft.IdentityModel.Claims;
using Newtonsoft.Json;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.Azure.ActiveDirectory.GraphClient.Extensions;
using System.Xml.Linq;
using Core.UserProfiles.Sync.Extensions;
namespace Core.UserProfiles.Sync
{
class Program
{
private const string UserProfilePrefix = "i:0#.f|membership|";
static void Main(string[] args)
{
try
{
//read the configuration data and setup tenant sp admin url
SyncConfiguration configuration = GetSyncConfiguration();
Uri sharePointAdminUri = new Uri(ConfigurationManager.AppSettings["TenantSharePointAdminUrl"]);
//query Azure AD for users
var users = GetUsersFromActiveDirectory(sharePointAdminUri).Result; //run query to AD
Console.WriteLine("Found " + users.Count + " users...");
//pass users and populate data
SetUserProfileDataWithUserContext(sharePointAdminUri, configuration, users);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Application finished...");
bool notClose = true;
bool.TryParse(ConfigurationManager.AppSettings["DoNotCloseConsole"], out notClose);
if (notClose)
{
Console.ReadKey();
}
}
/// <summary>
/// This method constructs an AD Client and gets all users with paging
/// </summary>
/// <returns>A list of IUser objects</returns>
private static async Task<List<GraphApi.IUser>> GetUsersFromActiveDirectory(Uri sharePointAdminUri)
{
//get the Active Directory client
var activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication(sharePointAdminUri);
List<GraphApi.IUser> usersList = new List<GraphApi.IUser>();
//get all users from AD with paging
IPagedCollection<GraphApi.IUser> pagedCollection = await activeDirectoryClient.Users.ExecuteAsync();
if (pagedCollection != null)
{
do //append pages to the list
{
usersList.AddRange(pagedCollection.CurrentPage.ToList());
pagedCollection = await pagedCollection.GetNextPageAsync();
//Below condition checks for the last batch of users and adds it to the list.
//Otherwise, the pagedCollection.MorePagesAvailable fails in the while condition and the loop misses the last batch of users.
if (pagedCollection!= null && !pagedCollection.MorePagesAvailable)
{
usersList.AddRange(pagedCollection.CurrentPage.ToList());
}
} while (pagedCollection != null && pagedCollection.MorePagesAvailable);
}
return usersList;
}
private static void SetUserProfileDataWithUserContext(Uri sharePointAdminUri, SyncConfiguration configuration, List<GraphApi.IUser> users)
{
string tenantAdminLoginName = ConfigurationManager.AppSettings["TenantAdminLogin"];
string tenantAdminPassword = ConfigurationManager.AppSettings["TenantAdminPassword"];
using (ClientContext clientContext = new ClientContext(sharePointAdminUri.ToString()))
{
//authenticate with SPOCredentials
SecureString password = new SecureString();
foreach (char c in tenantAdminPassword.ToCharArray()) password.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(tenantAdminLoginName, password);
clientContext.ExecuteQuery();
// Get the people manager instance for tenant context
PeopleManager peopleManager = new PeopleManager(clientContext);
foreach (GraphApi.User user in users)
{
foreach (Property prop in configuration.Properties)
{
try
{
UpdateProperty(peopleManager, user, prop);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
clientContext.ExecuteQueryWithExponentialRetry(10, 30000); //implemented with throttling
}
}
}
private static void UpdateProperty(PeopleManager peopleManager, GraphApi.User user, Property prop)
{
var propertyNewValue = typeof(GraphApi.User).GetProperty(prop.ADAttributeName).GetValue(user);
if (propertyNewValue != null || prop.WriteIfBlank)
{
if (prop.IsMulti)
{
peopleManager.SetMultiValuedProfileProperty(UserProfilePrefix + user.UserPrincipalName,
prop.UserProfileAttributeName, new List<string>() { });
}
else
{
peopleManager.SetSingleValueProfileProperty(UserProfilePrefix + user.UserPrincipalName,
prop.UserProfileAttributeName,
propertyNewValue == null ? string.Empty : propertyNewValue.ToString());
}
Console.WriteLine("Updated User: {0} Property: {1} New Value: {2}",
user.DisplayName, prop.UserProfileAttributeName, propertyNewValue);
}
//logic to write only if different
}
private static SyncConfiguration GetSyncConfiguration()
{
SyncConfiguration configuration = null;
string path = "PropertyConfiguration.xml";
XElement xml = XElement.Load(path);
var xmlSerializer = new XmlSerializer(typeof(SyncConfiguration));
configuration = (SyncConfiguration)xmlSerializer.Deserialize(xml.CreateReader());
return configuration;
}
}
}