0

I've been trying to get all users from a SharePoint site collection site.

But some people that I can retrieve using people picker, I can't find from the code. And when I try to get them using UserProfile user = userProfileMgr.GetUserProfile(targetAccountName), I get the exception:

An error was encountered while retrieving the user profile

List<String[]> users = new List<String[]>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
                                                     {


                string targetAccountName = "virtualsp\\b.ferreirarocha";
                using (SPSite site = new SPSite("http://sp2010devid/sites/EsercioWeekend/"))
                {
                    SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                    try
                    {

                        // Retrieve and iterate through all of the user profiles in this context.
                        Console.WriteLine("Retrieving user profiles:");
                        UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
                        IEnumerator userProfiles = userProfileMgr.GetEnumerator();
                        while (userProfiles.MoveNext())
                        {
                            UserProfile userProfile = (UserProfile)userProfiles.Current;
                            users.Add(userProfile.MultiloginAccounts);
                        }

                        // Retrieve a specific user profile. Change the value of a user profile property
                        // and save (commit) the change on the server.
                        UserProfile user = userProfileMgr.GetUserProfile(targetAccountName);
                        Console.WriteLine("\nRetrieving user profile for " + user.DisplayName + ".");
                        user.DisplayName = "Pat";
                        user.Commit();
                        Console.WriteLine("\nThe user\'s display name has been changed.");
                        Console.Read();
                    }

                    catch (System.Exception e)
                    {
                        Console.WriteLine(e.GetType().ToString() + ": " + e.Message);
                        Console.Read();
                    }
                }
  • I don't understand. Your code above has nearly nothing to do with getting users from a specific site collection? – Robert Lindgren Mar 10 '15 at 15:19
  • UserProfile i mean – hello B Mar 10 '15 at 15:25
  • i think your problem with TargetAccountName...you pass this value "virtualsp\\b.ferreirarocha", did you try domain\username" or you can try directly pass the user name: UserProfile userProfile = manager.GetUserProfile(@"Domain\userName"); – Waqas Sarwar MVP Mar 10 '15 at 15:59
  • yes And I gto the same error. For "virtualsp\\administrator" it works fine! – hello B Mar 10 '15 at 16:26
  • The people picker shows names both directly from AD and the user profile service. The UserProfileManager only queries the user profile service – Kevin Stone Mar 10 '15 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.