protected override IEnumerable <UserProfileChange> RetrieveDataObjects() { SPServiceContext context = null; if (ParameterSetName == "UPA") { SPSiteSubscriptionIdentifier subId; if (SiteSubscription != null) { SPSiteSubscription siteSub = SiteSubscription.Read(); subId = siteSub.Id; } else { subId = SPSiteSubscriptionIdentifier.Default; } SPServiceApplication svcApp = UserProfileServiceApplication.Read(); context = SPServiceContext.GetContext(svcApp.ServiceApplicationProxyGroup, subId); } else { using (SPSite site = ContextSite.Read()) { context = SPServiceContext.GetContext(site); } } UserProfileManager profileManager = new UserProfileManager(context); DateTime tokenStart = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(Interval)); UserProfileChangeToken changeToken = new UserProfileChangeToken(tokenStart); UserProfileChangeQuery changeQuery = new UserProfileChangeQuery(false, true); changeQuery.ChangeTokenStart = changeToken; changeQuery.UserProfile = true; changeQuery.SingleValueProperty = IncludeSingleValuePropertyChanges; changeQuery.MultiValueProperty = IncludeMultiValuePropertyChanges; changeQuery.Anniversary = IncludeAnniversaryChanges; changeQuery.Colleague = IncludeColleagueChanges; changeQuery.OrganizationMembership = IncludeOrganizationMembershipChanges; changeQuery.DistributionListMembership = IncludeDistributionListMembershipChanges; changeQuery.PersonalizationSite = IncludePersonalizationSiteChanges; changeQuery.SiteMembership = IncludeSiteMembershipChanges; UserProfileChangeCollection changes = profileManager.GetChanges(changeQuery); foreach (UserProfileChange change in changes) { WriteResult(change); } return(null); }
private List <string> GetChanges(SyncOptions options) { var users = new List <string>(); try { _changeToken = options.IgnoreChangeToken ? string.Empty : Settings.Default.ChangeToken; var changeTokenStart = new UserProfileChangeToken(); var profileChanges = _changeService.GetChanges(_changeToken, new UserProfileChangeQuery { ChangeTokenStart = changeTokenStart, Add = true, Update = true, UserProfile = true, SingleValueProperty = true, MultiValueProperty = true, }); Settings.Default.ChangeToken = profileChanges.ChangeToken; Settings.Default.Save(); var accountNameChanges = profileChanges.Changes.GroupBy(d => d.UserAccountName).Select(gr => gr.Key).ToList(); foreach (var accountChanged in accountNameChanges) { try { var userProfile = _ups.GetUserProfileByName(accountChanged); if (options.UserLimit > 0 && users.Count >= options.UserLimit) { return(users); } if (IsValidProfile(userProfile, options.UserAccountFilter)) { users.Add(FieldsToJson(userProfile)); } } catch (Exception ex) { SyncUtil.WriteLine("Error : {0} AccountChanged: {1}", ex.Message, accountChanged); } } } catch (Exception ex) { SyncUtil.WriteLine("IncrementalSyncGetChangesFailed : {0} {1}", ex.Message, ex.StackTrace); Settings.Default.ChangeToken = string.Empty; Settings.Default.Save(); } return(users); }
public List <User> List(DateTime date) { var users = new List <User>(); try { var changeToken = new UserProfileChangeToken(); var profileChanges = farmUserProfileChangeService.GetChanges(string.Empty, new UserProfileChangeQuery { ChangeTokenStart = changeToken, Add = true, Update = true, UserProfile = true, SingleValueProperty = true, MultiValueProperty = true, }); var accNameList = profileChanges.Changes.Where(ch => ch.EventTime >= date).GroupBy(d => d.UserAccountName).Select(gr => gr.Key).ToList(); foreach (var accName in accNameList) { try { var userProfileData = farmUserProfileService.GetUserProfileByName(accName); var user = new SPFarmUser(syncSettings.SPFarmUserIdFieldName, syncSettings.SPFarmUserEmailFieldName, userProfileData); InitUserFields(user, userProfileData); if (!string.IsNullOrEmpty(user.Email)) { users.Add(user); } } catch (Exception ex) { if (ex.Message.Contains("could not be found")) { SPLog.Event(string.Format("User with account name {0} could not be found in SharePoint.", accName)); continue; } SPLog.UserProfileUpdated(ex, string.Format("FarmUserProfileService.GetUserProfileByName() Failed: {0} {1}", ex.Message, ex.StackTrace)); } } } catch (Exception ex) { SPLog.UserProfileUpdated(ex, string.Format("FarmUserProfileService.List() Failed: {0} {1}", ex.Message, ex.StackTrace)); } return(users); }
/// <summary> /// Synchronizes the taxonomy fields from specified mappings for changed user profile in the last specified window of time. /// The source property will build the destination property taxonomy tree. /// </summary> /// <param name="site">The site for the service context.</param> /// <param name="changeStartDate">The start date for which to include user changes.</param> /// <param name="mappings">The taxonomy property mappings. The key is the source property name. The value is the destination property name.</param> public void SyncTaxonomyFieldsForChangedUsers(SPSite site, DateTime changeStartDate, IDictionary<string, string> mappings) { using (new SPMonitoredScope(string.Format(CultureInfo.InvariantCulture, "{0}::{1}", MonitoredScopePrefix, "SyncTaxonomyFieldsForChangedUsers"))) { // Get user profile objects var context = SPServiceContext.GetContext(site); var userProfileManager = new UserProfileManager(context); var profileSubtypeManager = ProfileSubtypeManager.Get(context); var profileSubtype = profileSubtypeManager.GetProfileSubtype( ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User)); var profileSubtypeProperties = profileSubtype.Properties; // For each property, update mapping foreach (var mapping in mappings) { var sourcePropertyName = mapping.Key; var targetPropertyName = mapping.Value; var srcSubtypeProp = profileSubtypeProperties.GetPropertyByName(sourcePropertyName); var targetSubtypeProp = profileSubtypeProperties.GetPropertyByName(targetPropertyName); if (srcSubtypeProp != null && targetSubtypeProp != null) { if (targetSubtypeProp.CoreProperty.TermSet != null && srcSubtypeProp.CoreProperty.TermSet != null) { if (targetSubtypeProp.CoreProperty.IsMultivalued) { // Get some subset of changes to a user profile since last 24h hours var startDate = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1)); var changeQuery = new UserProfileChangeQuery(false, true); var changeToken = new UserProfileChangeToken(startDate); changeQuery.ChangeTokenStart = changeToken; changeQuery.SingleValueProperty = true; changeQuery.MultiValueProperty = true; changeQuery.Update = true; changeQuery.Add = true; changeQuery.Delete = true; changeQuery.UpdateMetadata = true; var changes = userProfileManager.GetChanges(changeQuery); foreach ( var profile in changes.OfType<UserProfilePropertyValueChange>() .Select(change => (Microsoft.Office.Server.UserProfiles.UserProfile)change.ChangedProfile)) { this.SyncTaxonomyFields( site, profile, mapping, targetSubtypeProp.CoreProperty.TermSet.TermStore.Id, targetSubtypeProp.CoreProperty.TermSet.Id); } } else { this.logger.Error( string.Format( CultureInfo.InvariantCulture, @"UserProfileTargetingService.SyncTaxonomyFieldsForChangedUsers: The target property {0} is not set as multi valued. Please set this target property as multi valued", targetPropertyName)); } } else { this.logger.Error( @"UserProfileTargetingService.SyncTaxonomyFieldsForChangedUsers: Some properties have wrong TermSet configuration. Please review user properties taxonomy settings."); } } else { this.logger.Error( @"UserProfileTargetingService.SyncTaxonomyFieldsForChangedUsers: One or more user properties specified in the configuration list doesn't exists in the user profile property schema!"); } } } }