3

I am trying to get user profile image from user profile. Here is my code sample:

SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        SPServiceContext ctx = SPServiceContext.GetContext(site);
                        UserProfileManager upm = new UserProfileManager(ctx); // Here I get error
                        if (upm.UserExists(loginUserName))
                        {
                            UserProfile up = upm.GetUserProfile(loginUserName);

                            if (!string.IsNullOrWhiteSpace(Convert.ToString(up["PictureUrl"].Value)))
                            {
                                this.userimage = Convert.ToString(up["PictureUrl"].Value);
                            }                            
                        }
                    }
                });

But I get below error if user profile doesn't exist at userProfileManager declaration in above code:

An exception of type 'Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code

Additional information: UserProfileApplicationNotAvailableException_Logging :: UserProfileApplicationProxy.GetRawPartitionID has null proxy

Is there any other way through which I can check if user profile exist programmatically? Thanks in advance.

5
  • You might find your answer here: sharepoint.stackexchange.com/questions/10243/… Commented Apr 13, 2016 at 12:20
  • got a few of those. was a problem in the user profile service configuration at the farm level Commented Apr 13, 2016 at 12:39
  • @George - I have already gone through it but no luck. Commented Apr 14, 2016 at 7:22
  • @Tiago - I am not getting error when user profile exists for user. This issue only occurs if user profile do not exist. Commented Apr 14, 2016 at 7:23
  • well from what you said you get the error before the line that uses the login name. if that's true, it shouldn't matter whether or not the user exists. also I dont get any error so I stand by what I said. problems in the service. e.g. stackoverflow.com/questions/4428552/… Commented Apr 14, 2016 at 9:45

2 Answers 2

3

Your code is fine.

I would not attempt to circumvent the problem as it may be telling you that there is in fact something wrong with your setup.

There are many reports (I've experienced this myself) where the User Profile Service is throwing exceptions and needs to be brought back to full health.

e.g.

Go to Central Admin -> Manage Web Applications. Find the Web App hosting your MySites and click on it. Go to Service Connections and validate your UPA Proxy is selected here.

https://stackoverflow.com/questions/4428552/user-profile-application-proxy-failed-to-retrieve-partitions-from-user-profile-a

or

This issue happens mostly on the Farm installation. Everyone will say to recreate the UPS, restart the server and then it should work fine. But if you have used the Custom properties and created a managed property in Search then its very difficult to recreate it and then to create it again. The better solution so far i have researched is the following

  1. Stop the User Profile Synchronization service

  2. Stop the User Profile Service

  3. Start the User Profile Service

  4. Start the User Profile Synchronization service.

https://stackoverflow.com/questions/4428552/user-profile-application-proxy-failed-to-retrieve-partitions-from-user-profile-a

2
  • Can we check some other way that user profile exist or not? Commented Apr 15, 2016 at 8:55
  • User interface: CA > Service Applications > User Profile Sevice > Manager User Profiles. Programatically you may always get that error if you query the UPS, but you can try querying the webservice remotely: github.com/tiagoduarte/sp-ups otherwise you can look for Web.SiteUsers or something rather than UPS, depending on what you are trying to do Commented Apr 15, 2016 at 9:03
2

You could use try catch. If it always crashes.

Not the cleanest way of handling it but this should do it. But I would call this a quickfix.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    {
        SPServiceContext ctx = SPServiceContext.GetContext(site);
        try
        {
             UserProfileManager upm = new UserProfileManager(ctx); // Here I get error
             if (upm.UserExists(loginUserName))
             {
                  UserProfile up = upm.GetUserProfile(loginUserName);
                  if(!string.IsNullOrWhiteSpace(Convert.ToString(up["PictureUrl"].Value)))
                  {
                     this.userimage = Convert.ToString(up["PictureUrl"].Value);
                  }
             }
         }
         catch (Exception)
         {
            //Handle the problem here..
         }
     }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.