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);
        }
        /// <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!");
                    }
                }
            }
        }