gregor.tusar
03/06/2025, 8:41 AMusync
archive.
The problem is that potentially the members were using both websites and in some cases the usernames for one member are different, but the emails are the same. So with current approach I get duplicated email members which is obviously wrong.
Is there any uSync extension where I could override the matching of the user not to be on username but rather on email?
I was checking this functionality https://docs.jumoo.co.uk/usync/complete/guides/Exporter/#validating-sync-pack
Is it possible to "edit" the import pack before importing?
We are using Umbraco (13.7.0), uSync Complete (13.1.9) and it's Exporter functionality.gregor.tusar
03/06/2025, 4:00 PMgregor.tusar
03/07/2025, 11:03 AMuSyncImportingItemNotification
and the users are not duplicated anymore:
cs
public class MemberImportNotificationHandler(IMemberService memberService, ILogger<MemberImportNotificationHandler> logger) : INotificationHandler<uSyncImportingItemNotification>
{
public void Handle(uSyncImportingItemNotification notification)
{
if (notification.Item is null)
{
return;
}
// import doc validation only for Member type
if (notification.Item.Name == nameof(Member))
{
var memberImportDoc = notification.Item;
var memberEmail = memberImportDoc.Element("Info")?.Element("Email")?.Value;
if (!string.IsNullOrEmpty(memberEmail))
{
var existingUser = memberService.GetByEmail(memberEmail);
if (existingUser is not null)
{
// check if username differs
var userName = memberImportDoc.Element("Info")?.Element("Username")?.Value;
// replace existing username with the on in import file
if (existingUser.Username != userName)
{
// update key and username
memberImportDoc.SetAttributeValue(nameof(Member.Key), existingUser.Key);
memberImportDoc.Element("Info")!.Element("Username")!.SetValue(existingUser.Username);
}
}
}
}
}
}
cs
builder.AddNotificationHandler<uSyncImportingItemNotification, MemberImportNotificationHandler>();
But now Properties
properties from the XML file are not updated in the existing member in import. Properties in Info
(Name, Created date) are updated.
Would maybe @Kevin Jump know how to solve this issue?gregor.tusar
03/07/2025, 1:56 PMcs
public class MemberImportNotificationHandler(IMemberService memberService) : INotificationHandler<uSyncImportingItemNotification>
{
public void Handle(uSyncImportingItemNotification notification)
{
// import doc validation only for Member type
if (notification.Item?.Name == "Member")
{
var memberImportDoc = notification.Item;
// get importing member email
var memberEmail = memberImportDoc.Element("Info")?.Element("Email")?.Value;
if (!string.IsNullOrEmpty(memberEmail))
{
// try find existing user
var existingMember = memberService.GetByEmail(memberEmail);
if (existingMember != null)
{
var importingMemberUserName = memberImportDoc.Element("Info")?.Element("Username")?.Value;
// replace existing username with the on in import file
if (existingMember.Username != importingMemberUserName)
{
// manually update member properties in Umbraco
var memberImportProperties = memberImportDoc.Element("Properties");
if (memberImportProperties != null)
{
bool memberUpdated = false;
// zip code
var zipCode = memberImportProperties.Element("zipCode")?.Value;
if (zipCode != existingMember.GetValue("zipCode")?.ToString())
{
existingMember.SetValue("zipCode", zipCode);
memberUpdated = true;
}
// country
var country = memberImportProperties.Element("country")?.Value;
if (country != existingMember.GetValue("country")?.ToString())
{
existingMember.SetValue("country", country);
memberUpdated = true;
}
if (memberUpdated)
{
memberService.Save(existingMember);
// cancel import, because it was updated manually
notification.Cancel = true;
}
}
}
}
}
}
}