1

I have an account that has been persisted in a database using the User Principal Name (UPN) format: jdoe@domain.gobalx.com

I am working in a SharePoint environment that is Claims authenticated using the UPN format.

My problem is I need to get a UserProfile object for the persisted UPN account. I have tried the following but it doesn't work:

string upnAccount = "jdoe@domain.globalx.com";
SPServiceContext ctx = SPServiceContext.GetContext(SPContext.Current.Site);
UserProfileManager upm = new UserProfileManager(ctx);
UserProfile user = upm.GetUserProfile(upnAccount);

I keep getting: Microsoft.Office.Server.UserProfiles.UserNotFoundException: An error was encountered while retrieving the user profile

Does this mean that I have to convert the UPN account into a claim, and if so does anyone have an example on how to do that?

2 Answers2

1
            UserProfileManager UPM = null;

            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    ServerContext serverContext = ServerContext.GetContext(site);
                    UPM = new UserProfileManager(serverContext);
                     foreach (UserProfile profile in UPM)
                    {
                        
                        an = profile["AccountName"].Value;
                        title = profile["Title"].Value;
                    }
                }
            }

You can try this for get all userprofile. In foreach loop, you can check for your fields and get particular user details.

halfer
  • 19,824
  • 17
  • 99
  • 186
V_B
  • 1,571
  • 5
  • 16
  • 37
0

In some cases under SharePoint sites with federated authentication there are no user profiles automatically created until you haven't setup synchronization. For this issue you may check if there already exists user profile for jdoe@domain.globalx.com via Central Admin.

Also your code must be run under impersonation, check the log for exception and try to use SPSecurity.RunWithElevatedPrivileges.

Oleg Savelyev
  • 968
  • 2
  • 8
  • 12