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); } } }
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); }
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); }
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); }