コード例 #1
0
        protected void MergeAndUpdate(InternalApi.Entities.TEApiUser internalUser, List <string> internalFields, InternalApi.Entities.User externalUser, List <string> externalFields, List <InternalApi.Entities.UserFieldMapping> mappedFields)
        {
            var mergeResult = ExecuteProfileSyncHelper.MergeUsers(externalUser, internalUser, mappedFields);

            try
            {
                if ((mergeResult & ExecuteProfileSyncHelper.MergeResult.InternalUpdated) == ExecuteProfileSyncHelper.MergeResult.InternalUpdated)
                {
                    ExecuteProfileSyncHelper.UpdateInternalUser(internalUser, internalFields);
                }
            }
            catch (Exception ex)
            {
                var msg = string.Format("Error updating internal user. User id = {0}/{1}", internalUser.Id, internalUser.Email ?? string.Empty);
                SPLog.UserProfileUpdated(ex, msg);
            }

            try
            {
                if ((mergeResult & ExecuteProfileSyncHelper.MergeResult.ExternalUpdated) == ExecuteProfileSyncHelper.MergeResult.ExternalUpdated)
                {
                    ProfileSyncService.Update(externalUser, externalFields);
                }
            }
            catch (Exception ex)
            {
                var msg = string.Format("Error updating external user. User id = {0}/{1}", externalUser.Id, externalUser.Email ?? string.Empty);
                SPLog.UserProfileUpdated(ex, msg);
            }
        }
コード例 #2
0
        public static bool GetLastRunTime(int providerId, out DateTime?lastRunTime, out Status syncStatus)
        {
            lastRunTime = null;
            syncStatus  = Status.Failed;

            const string spGetLastRunTime = "te_SharePoint_ProfileSync_GetLastRunTime";

            try
            {
                var providerIdParam  = new SqlParameter("@ProviderId", providerId);
                var lastRunTimeParam = new SqlParameter("@LastRunTime", System.Data.SqlDbType.DateTime)
                {
                    Direction = System.Data.ParameterDirection.Output
                };
                var syncStatusParam = new SqlParameter("@SyncStatus", System.Data.SqlDbType.Int)
                {
                    Direction = System.Data.ParameterDirection.Output
                };

                ExecuteScalar(spGetLastRunTime, GetConnection(), providerIdParam, lastRunTimeParam, syncStatusParam);

                lastRunTime = lastRunTimeParam.Value as DateTime?;
                syncStatus  = (Status)syncStatusParam.Value;

                return(true);
            }
            catch (Exception ex)
            {
                SPLog.UserProfileUpdated(ex, string.Format("Profile Sync Error. {0}: {1}", spGetLastRunTime, providerId));
            }
            return(false);
        }
コード例 #3
0
        private void InitUserFields(SPFarmUser user, IEnumerable <PropertyData> userProfileData)
        {
            const string log          = "Null PropertyData object found in User Profile while initializing user fields.";
            var          propertyName = string.Empty;

            foreach (var propertyData in userProfileData)
            {
                try
                {
                    if (propertyData == null)
                    {
                        PublicApi.Eventlogs.Write(log, new EventLogEntryWriteOptions {
                            Category = "SharePoint"
                        });
                        continue;
                    }

                    propertyName = propertyData.Name;

                    var name = propertyName;
                    user.Fields.Add(name, GetSanitizeUserFieldValue(propertyData));
                }
                catch (Exception ex)
                {
                    var msg = string.Format("FarmUserProfileService.InitUserFields() Error on property: {0} {1}", propertyName, ex.StackTrace);
                    SPLog.UserProfileUpdated(ex, msg);
                }
            }
        }
コード例 #4
0
        public List <User> List(ref int nextIndex)
        {
            var users = new List <User>();

            try
            {
                GetUserProfileByIndexResult userInstance;
                if (nextIndex <= 0)
                {
                    // start index for the Profile Web Service
                    nextIndex = -1;
                }

                do
                {
                    userInstance = farmUserProfileService.GetUserProfileByIndex(nextIndex);
                    if (userInstance == null || userInstance.UserProfile == null)
                    {
                        continue;
                    }

                    try
                    {
                        var user = new SPFarmUser(syncSettings.SPFarmUserIdFieldName, syncSettings.SPFarmUserEmailFieldName, userInstance.UserProfile);
                        InitUserFields(user, userInstance.UserProfile);
                        if (!string.IsNullOrEmpty(user.Email))
                        {
                            users.Add(user);
                        }
                    }
                    catch (Exception ex)
                    {
                        SPLog.Event(string.Format("Error : {0} FieldCount: {1} Fields: {2}",
                                                  ex.Message,
                                                  userInstance.UserProfile.Length,
                                                  ProfileFieldDump(userInstance.UserProfile)));
                    }

                    var nextValue = userInstance.NextValue ?? string.Empty;

                    if (!int.TryParse(nextValue.Replace(",", ""), out nextIndex))
                    {
                        SPLog.Event(string.Format("Error with next index : {0}", nextValue));
                    }
                }while (userInstance != null && userInstance.UserProfile != null && users.Count < userProfileBatchCapacity);
            }
            catch (Exception ex)
            {
                SPLog.UserProfileUpdated(ex, string.Format("FarmUserProfileService.List() Failed: {0} {1}", ex.Message, ex.StackTrace));
            }
            return(users);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        public static void SetLastRunStatus(int providerId, Status syncStatus)
        {
            const string spResetLastRunStatus = "te_SharePoint_ProfileSync_SetLastRunStatus";

            try
            {
                var providerIdParam = new SqlParameter("@ProviderId", providerId);
                var syncStatusParam = new SqlParameter("@SyncStatus", (int)syncStatus);
                ExecuteNonQuery(spResetLastRunStatus, GetConnection(), providerIdParam, syncStatusParam);
            }
            catch (Exception ex)
            {
                SPLog.UserProfileUpdated(ex, string.Format("Profile Sync Error. {0}: {1}", Enum.GetName(typeof(Status), syncStatus), providerId));
            }
        }
コード例 #7
0
        public List <User> List(IEnumerable <string> emails)
        {
            // Load account names using spcontext
            var accNameList = new List <string>();
            var web         = spcontext.Site.RootWeb;

            foreach (var camlQuery in CamlQueryBuilder(emails.ToArray(), userProfileBatchCapacity, syncSettings.SPUserEmailFieldName))
            {
                var spuserCollection = web.SiteUserInfoList.GetItems(new CamlQuery {
                    ViewXml = camlQuery
                });
                spcontext.Load(spuserCollection, uList => uList.Include(u => u["Name"]));
                spcontext.ExecuteQuery();
                accNameList.AddRange(from spuser in spuserCollection where spuser["Name"] != null select spuser["Name"].ToString());
            }

            var users = new List <User>();

            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") || ex.StackTrace.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));
                }
            }
            return(users);
        }
コード例 #8
0
        public static void ResetLastRunTime(int providerId, Status syncStatus)
        {
            const string spResetLastRunTime = "te_SharePoint_ProfileSync_ResetLastRunTime";

            try
            {
                var providerIdParam  = new SqlParameter("@ProviderId", providerId);
                var syncStatusParam  = new SqlParameter("@SyncStatus", (int)syncStatus);
                var lastRunTimeParam = new SqlParameter("@LastRunTime", System.Data.SqlDbType.DateTime)
                {
                    Direction = System.Data.ParameterDirection.Output
                };
                ExecuteNonQuery(spResetLastRunTime, GetConnection(), providerIdParam, lastRunTimeParam, syncStatusParam);
            }
            catch (Exception ex)
            {
                SPLog.UserProfileUpdated(ex, string.Format("Profile Sync Error. {0}: {1}", spResetLastRunTime, providerId));
            }
        }
コード例 #9
0
        public static MergeResult MergeUsers(InternalApi.Entities.User externalUser, InternalApi.Entities.User internalUser, IEnumerable <InternalApi.Entities.UserFieldMapping> mapping)
        {
            var res          = MergeResult.None;
            var hasNullField = false;

            try
            {
                foreach (var map in mapping)
                {
                    var internalFieldVal = internalUser[map.InternalUserFieldId];
                    var externalFieldVal = externalUser[map.ExternalUserFieldId];

                    hasNullField |= internalFieldVal == null;

                    if (map.SyncDirection == InternalApi.Entities.SyncDirection.Export && internalFieldVal != null && !internalFieldVal.Equals(externalFieldVal))
                    {
                        internalUser[map.InternalUserFieldId] = externalUser[map.ExternalUserFieldId];
                        res |= MergeResult.InternalUpdated;
                    }

                    if (map.SyncDirection == InternalApi.Entities.SyncDirection.Import && externalFieldVal != null && !externalFieldVal.Equals(internalFieldVal))
                    {
                        externalUser[map.ExternalUserFieldId] = internalUser[map.InternalUserFieldId];
                        res |= MergeResult.ExternalUpdated;
                    }
                }
            }
            catch (Exception ex)
            {
                SPLog.UserProfileUpdated(ex, String.Format("Could Not Merge User: {0}/{1}", internalUser.Id, internalUser.Email ?? String.Empty));
            }

            if (res == MergeResult.None && hasNullField)
            {
                SPLog.Info(string.Format("Profile Sync no fields merged for ({0}:{1}). Please verify __CommunityServer__Service__ contains the Administrators role.", internalUser.Id, internalUser.Email));
            }

            return(res);
        }